package iku.os.apkManager;

import static java.lang.Thread.sleep;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.io.File;
import java.io.IOException;

import iku.os.FileUtils.FileUtils;
import iku.os.ShellUtils.ShellUtils;

public class apk {
    private static final String TAG = "IkuClient apkManager";
    private static String apk_path = "/sdcard/apks";
    private static String appName;
    private static String oldpath = apk_path + appName;
    private static String name,baoming, zip_name, newpath;

    public static boolean clientInstall(File apkFile) {
        Log.i(TAG, "clientInstall : " + apkFile);
        try {
            String chmodCmd = "chmod 777 " + apkFile;
            ShellUtils.execCommand(chmodCmd,true);
            String installCmd = "pm install -r" + apkFile;
            ShellUtils.execCommand(installCmd,true);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "clientInstall eror:",e);
            e.printStackTrace();
        }
        return false;
    }


    public static boolean clientUninstall(String packageName) {
        Log.i(TAG, "start to clientuninstall : " + packageName);
        try {
            String cmd = "pm uninstall " + packageName;
            ShellUtils.execCommand(cmd,true);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "clientUninstall eror: ",e);
            e.printStackTrace();
        }
        return false;
    }


    public static boolean checkAppInstalled(Context context, String pkgName) {
        if (pkgName == null || pkgName.isEmpty()) {
            return false;
        }
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
//            e.printStackTrace();
        }
        boolean exists = false;
        if (packageInfo == null) {
            exists = false;
        } else {
            exists = true;//true为安装了，false为未安装
        }

        Log.i(TAG, "checkAppInstalled(" + pkgName + ") : " + exists);

        return exists;
    }

    private static void changeZip() {

        String[] split = appName.split(".xapk");
        //分割后选择保留那段
        zip_name = split[0];
        Log.e("压缩包名", zip_name);


        oldpath = apk_path + "/" + name;
        Log.i("oldpath", oldpath);
        newpath = apk_path + "/" + zip_name + ".zip";
        Log.i("newpath", newpath);

        //将xapk换成zip格式
        FileUtils.renameFile(oldpath, newpath);

        //进行解压
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    sleep(3000);

                    try {
                        FileUtils.upZipFile(new File(newpath), apk_path + "/" + zip_name);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                Log.e("zip：", "文件解压成功");
            }
        }).start();

    FileUtils.renameFile(newpath, oldpath);

    }
    

}
