package com.dankal.dankal_launcher;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.launcher3.debug.DebugInfoActivity;
import com.android.launcher3.debug.SearchAppCanUseActivity;
import com.android.launcher3.manager.LauncherManager;
import com.android.launcher3.util.PermissionUtil;

public class PermissionSettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_setting);
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        check();
    }

    //激活设备管理
    private ConstraintLayout clActivateDeviceManager;
    private ImageView ivActivateDeviceManager;
    //关闭耗电保护
    private ConstraintLayout clClosePower;
    private ImageView ivClosePower;
    //开启查看应用使用权限
    private ConstraintLayout clOpenApplicationPermission;
    private ImageView ivOpenApplicationPermission;
    //关闭USB测试
    private ConstraintLayout clCloseUsbTest;
    private ImageView ivCloseUsbTest;
    //开启通知栏监听
    private ConstraintLayout clOpenNotificationListener;
    private ImageView ivOpenNotificationListener;
    //开启自启动管理
//    private ConstraintLayout clOpenAutoStartManager;
//    private ImageView ivOpenAutoStartManager;
    //设置默认桌面
    private ConstraintLayout clSettingDefaultLauncher;
    private ImageView ivSettingDefaultLauncher;
    //关闭应用分身
//    private ConstraintLayout clCloseApplication;
//    private ImageView ivCloseApplication;

    private ConstraintLayout clSelfStartingPermissions;
    private ImageView ivSelfStartingPermissions;

    private Button buttonConfirm;

    private void initData() {
        LauncherManager.getInstance().setInterceptSystemSetting(false);
        LauncherManager.getInstance().changeExitState(true);
        ClickAction clickAction = new ClickAction();
        clActivateDeviceManager = findViewById(R.id.cl_activate_device_manager);
        ivActivateDeviceManager = findViewById(R.id.iv_activate_device_manager);
        clClosePower = findViewById(R.id.cl_close_power);
        ivClosePower = findViewById(R.id.iv_close_power);
        clOpenApplicationPermission = findViewById(R.id.cl_open_application_permission);
        ivOpenApplicationPermission = findViewById(R.id.iv_open_application_permission);
        clCloseUsbTest = findViewById(R.id.cl_close_usb_test);
        ivCloseUsbTest = findViewById(R.id.iv_close_usb_test);
        clSelfStartingPermissions = findViewById(R.id.cl_self_starting_permissions);
        ivSelfStartingPermissions = findViewById(R.id.iv_self_starting_permissions);
        clOpenNotificationListener = findViewById(R.id.cl_open_notification_listener);
        ivOpenNotificationListener = findViewById(R.id.iv_open_notification_listener);
//        clOpenAutoStartManager = findViewById(R.id.cl_open_auto_start_manager);
//        ivOpenAutoStartManager = findViewById(R.id.iv_open_auto_start_manager);
        clSettingDefaultLauncher = findViewById(R.id.cl_setting_launcher);
        ivSettingDefaultLauncher = findViewById(R.id.iv_setting_launcher);
//        clCloseApplication = findViewById(R.id.cl_close_application);
//        ivCloseApplication = findViewById(R.id.iv_close_application);
        buttonConfirm = findViewById(R.id.button_confirm);
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //mPresenter.addTimeSwitch();
                LauncherManager.getInstance().setInterceptSystemSetting(true);
                LauncherManager.getInstance().changeExitState(false);
            }
        });

        findViewById(R.id.tv_searchAppCanUseActivity).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(PermissionSettingActivity.this, SearchAppCanUseActivity.class));
                    }
                }
        );

        findViewById(R.id.tv_debugInfoActivity).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(PermissionSettingActivity.this, DebugInfoActivity.class));

                    }
                }
        );
        clActivateDeviceManager.setOnClickListener(clickAction);
        clClosePower.setOnClickListener(clickAction);
        clOpenApplicationPermission.setOnClickListener(clickAction);
        clCloseUsbTest.setOnClickListener(clickAction);
        clOpenNotificationListener.setOnClickListener(clickAction);
//        clOpenAutoStartManager.setOnClickListener(clickAction);
        clSettingDefaultLauncher.setOnClickListener(clickAction);
        clSelfStartingPermissions.setOnClickListener(clickAction);

//        clCloseApplication.setOnClickListener(clickAction);

    }

    private void check() {
        //判断是否有设备管理权限
        boolean activateDeviceManager;
        if (PermissionUtil.checkDeviceAdminPermission(this)) {
            openFailTick(ivActivateDeviceManager);
            clActivateDeviceManager.setEnabled(false);
            activateDeviceManager = true;
        } else {
            clActivateDeviceManager.setEnabled(true);
            openFailure(ivActivateDeviceManager);
            activateDeviceManager = false;
        }
        boolean closePower;
        //判断是否耗电保护
        if (PermissionUtil.checkBatteryOptimize(this)) {
            openFailTick(ivClosePower);
            clClosePower.setEnabled(false);
            closePower = true;
        } else {
            openFailure(ivClosePower);
            clClosePower.setEnabled(true);
            closePower = false;
        }

        boolean selfStarting;
        //自启动
        if (PermissionUtil.checkAutoStartUp(this)) {
            openFailTick(ivSelfStartingPermissions);
            clSelfStartingPermissions.setEnabled(false);
            selfStarting = true;
        } else {
            openFailure(ivSelfStartingPermissions);
            clSelfStartingPermissions.setEnabled(true);
            selfStarting = false;
        }

        boolean openApplicationPermission;
        //判断是否开启查看应用使用权限
        if (PermissionUtil.checkAppUsagePermission(this)) {
            openFailTick(ivOpenApplicationPermission);
            clOpenApplicationPermission.setEnabled(false);
            openApplicationPermission = true;
        } else {
            openFailure(ivOpenApplicationPermission);
            clOpenApplicationPermission.setEnabled(true);
            openApplicationPermission = false;
        }
        boolean closeUsbTest;
        //判断是否开启usb权限
//        测试的时候
//        clCloseUsbTest.setEnabled(false);
//        closeUsbTest = true;
        //打包的时候
        if (!PermissionUtil.enAbleADB(this)) {
            openFailTick(ivCloseUsbTest);
            clCloseUsbTest.setEnabled(false);
            closeUsbTest = true;
        } else {
            openFailure(ivCloseUsbTest);
            clCloseUsbTest.setEnabled(true);
            closeUsbTest = false;
        }

        boolean openNotificationListener;
        //判断是否开启通知栏
        if (PermissionUtil.isNotificationListenersEnabled(this)) {

            openFailTick(ivOpenNotificationListener);
            clOpenNotificationListener.setEnabled(false);
            openNotificationListener = true;
        } else {
            openFailure(ivOpenNotificationListener);
            clOpenNotificationListener.setEnabled(true);
            openNotificationListener = false;
        }

        boolean settingDefaultLauncher;
        //判断是否设置为默认桌面
        if (PermissionUtil.isDefaultHome(this)) {

            openFailTick(ivSettingDefaultLauncher);
            clSettingDefaultLauncher.setEnabled(false);
            settingDefaultLauncher = true;
        } else {
            openFailure(ivSettingDefaultLauncher);
            clSettingDefaultLauncher.setEnabled(true);
            settingDefaultLauncher = false;
        }

        buttonConfirm.setEnabled(activateDeviceManager && closePower && openApplicationPermission &&
                closeUsbTest && openNotificationListener && settingDefaultLauncher);
//        //判断是否应用分身
//        if (!PermissionUtil.isDualApk(this)) {
//            openFailTick(ivCloseApplication);
//            clCloseApplication.setEnabled(false);
//        } else {
//            openFailure(ivCloseApplication);
//            clCloseApplication.setEnabled(true);
//        }
    }

//    private void startOpen(ImageView imageView) {
//        openLoad(imageView);
//    }

    private void openFailure(ImageView imageView) {
        imageView.setImageResource(R.mipmap.ic_failure);
    }

    private void openFailTick(ImageView imageView) {
        imageView.setImageResource(R.mipmap.ic_tick);
    }


//    private void openLoad(ImageView imageView) {
//        imageView.setImageResource(R.mipmap.ic_loading);
//        ObjectAnimator icon_anim = ObjectAnimator.ofFloat(imageView, "rotation", 0.0F, 3600F);
//        icon_anim.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                super.onAnimationEnd(animation);
//                Logger.e("addListener。onAnimationEnd");
//                buttonConfirm.setEnabled(true);
//                openFailTick(imageView);
//            }
//        });
//        icon_anim.setDuration(3000);
//        RxAnimationTool.start(icon_anim);
//    }

    class ClickAction implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (R.id.cl_self_starting_permissions == id) {//自启动权限
                PermissionUtil.jumpStartInterface(PermissionSettingActivity.this);
            } else if (R.id.cl_activate_device_manager == id) {
                //设备管理
                PermissionUtil.gotoDeviceAdminPermissionSetting(PermissionSettingActivity.this);
            } else if (R.id.cl_close_power == id) {
                //关闭耗电保护
                PermissionUtil.gotoBatteryOptimize(PermissionSettingActivity.this);
            } else if (R.id.cl_open_application_permission == id) {
                //开启使用权限
                PermissionUtil.gotoAppUsagePermission(PermissionSettingActivity.this);
            } else if (R.id.cl_close_usb_test == id) {
                //关闭usb
                PermissionUtil.gotoDeveloperChoice(PermissionSettingActivity.this);

            } else if (R.id.cl_open_notification_listener == id) {
                //通知
                PermissionUtil.goToNotificationAccessSetting(PermissionSettingActivity.this);
            } else if (R.id.cl_setting_launcher == id) {
                //设置默认桌面
                PermissionUtil.setDefaultL(PermissionSettingActivity.this);
//                Utils.openResolverLauncher(PermissionSettingActivity.this);
                //设置默认桌面
            }
        }
    }
}
