package com.dankal.dankal_launcher;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.launcher3.Launcher;
import com.android.launcher3.manager.ColumnDef;
import com.android.launcher3.manager.LauncherManager;
import com.android.launcher3.manager.ManagementInfo;
import com.android.launcher3.manager.TimeModeManager;
import com.android.launcher3.model.TimeConfig;
import com.android.launcher3.model.TimeRegion;
import com.android.launcher3.util.KToast;
import com.android.launcher3.util.Logger;
import com.android.launcher3.util.PermissionUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import androidx.annotation.Nullable;

public class PackageManageActivity extends Activity {

    public static final String TAG = "PackageManageActivity";

    private TextView tvAdd, tvRemove;

    private TextView tvLock, tvUnlock;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    List<Long> regions = new ArrayList<>();
                    regions.add(Long.valueOf(9 * 3600));
                    regions.add(Long.valueOf(10 * 3600));

                    regions.add(Long.valueOf(10 * 3600));
                    regions.add(Long.valueOf(11 * 3600));

                    regions.add(Long.valueOf(11 * 3600));
                    regions.add(Long.valueOf(12 * 3600));

                    regions.add(Long.valueOf(12 * 3600));
                    regions.add(Long.valueOf(13 * 3600));

                    regions.add(Long.valueOf(13 * 3600));
                    regions.add(Long.valueOf(14 * 3600));

                    regions.add(Long.valueOf(15 * 3600));
                    regions.add(Long.valueOf(17 * 3600));

                    TimeConfig config = new TimeConfig.Builder(ColumnDef.TimeManager.WORK_DAY)
                            .lockScreenMode(ColumnDef.LockScreen.MODE_REST)
                            .addUseRegion(regions)
                            .addAppTypeConfig(ColumnDef.AppUseType.GAME_LOCK_TYPE, 46800)
                            .build();
                    LauncherManager.getInstance().addWorkMode(config);
                    break;
                case 1:
//
                    ManagementInfo managementInfo = new ManagementInfo();
                    managementInfo.setUseEndTimeStamp(43320);

                    managementInfo.setPackageName("com.tencent.mobileqq");
                    managementInfo.setLock(ColumnDef.LockApp.LOCKED);
                    managementInfo.setUseType(ColumnDef.AppUseType.GAME_LOCK_TYPE);

                    LauncherManager.getInstance().addPackageInfo(managementInfo);
                    Logger.e(TAG, "AFTER\t" + LauncherManager.getInstance().getManageAppInfo("com.tencent.mobileqq").toString());
                    break;

                case 2:
                    ManagementInfo managementInfo1 = new ManagementInfo();
                    managementInfo1.setUseEndTimeStamp(43320);

                    managementInfo1.setPackageName("com.tencent.mobileqq");
                    managementInfo1.setLock(ColumnDef.LockApp.LOCKED);
                    managementInfo1.setUseType(ColumnDef.AppUseType.GAME_LOCK_TYPE);

                    LauncherManager.getInstance().addPackageInfo(managementInfo1);
                    break;
            }


//            LauncherManager.getInstance().setCurrentWorkMode(ColumnDef.TimeManager.WORK_DAY);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_package_manage);

        LauncherManager.getInstance().changeExitState(true);
        LauncherManager.getInstance().setInterceptSystemSetting(false);

        tvAdd = findViewById(R.id.tv_add);

        tvRemove = findViewById(R.id.tv_remove);

        tvLock = findViewById(R.id.tv_lock);

        tvUnlock = findViewById(R.id.tv_unlock);

        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                List<Long> regions = new ArrayList<>();
//                regions.add(Long.valueOf(9 * 3600));
//                regions.add(Long.valueOf(10 * 3600));
//                TimeConfig config = new TimeConfig.Builder(ColumnDef.TimeManager.WORK_DAY)
//                        .lockScreenMode(ColumnDef.LockScreen.MODE_REST)
//                        .addUseRegion(regions)
//                        .addAppTypeConfig(ColumnDef.AppUseType.GAME_LOCK_TYPE, 60)
//                        .build();
//                LauncherManager.getInstance().addWorkMode(config);
//
//
//                LauncherManager.getInstance().setCurrentWorkMode(ColumnDef.TimeManager.WORK_DAY);
            }
        });


        tvRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ManagementInfo managementInfo = new ManagementInfo();
//
//                managementInfo.setUseEndTimeStamp(30);
//
//                managementInfo.setPackageName("com.cubic.autohome");
//                managementInfo.setLock(ColumnDef.LockApp.DEADLINE);
//                managementInfo.setUseType(ColumnDef.AppUseType.GAME_LOCK_TYPE);
//
//                LauncherManager.getInstance().addPackageInfo(managementInfo);


                managementInfo.setUseEndTimeStamp(3600);

                managementInfo.setPackageName("com.tencent.mm");
                managementInfo.setLock(ColumnDef.LockApp.DEADLINE);
                managementInfo.setUseType(ColumnDef.AppUseType.GAME_LOCK_TYPE);

                LauncherManager.getInstance().addPackageInfo(managementInfo);
                handler.sendEmptyMessageDelayed(1, 500);
                Logger.e(TAG, "before\t" + LauncherManager.getInstance().getManageAppInfo("com.tencent.mm").toString());


            }
        });


        tvLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                LauncherManager.getInstance().forceLockScreenForever(PackageManageActivity.this);
//                handler.sendEmptyMessageDelayed(1,1000*15);
//                PermissionUtil.setDefaultL(getApplicationContext());
                PermissionUtil.gotoAppUsagePermission(getApplicationContext());
            }
        });

        tvUnlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessageDelayed(2, 10000);
            }
        });


        findViewById(R.id.jump_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
<<<<<<< HEAD

                       /* LauncherManager.getInstance().changeExitState(true);
                        LauncherManager.getInstance().setInterceptSystemSetting(false);*/
                        PermissionUtil.gotoBatteryOptimize(getApplicationContext());
//                        handler.sendEmptyMessage(3);
=======
                        LauncherManager.getInstance().changeExitState(true);
                        LauncherManager.getInstance().setInterceptSystemSetting(false);
>>>>>>> 1
                    }
                }).start();
            }
        });


//        Date date = new Date();
//
//        date.setTime(System.currentTimeMillis());
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        int week = calendar.get(Calendar.DAY_OF_WEEK);
//
//        Logger.e(TAG, week + "\t" + (calendar.getFirstDayOfWeek() == Calendar.SUNDAY));
        List<Long> regions = new ArrayList<>();

        regions.add(Long.valueOf(11 * 3600));
        regions.add(Long.valueOf(12 * 3600));

        regions.add(Long.valueOf(13 * 3600));
        regions.add(Long.valueOf(14 * 3600));

        regions.add(Long.valueOf(15 * 3600));
        regions.add(Long.valueOf(17 * 3600));

        TimeConfig config = new TimeConfig.Builder(ColumnDef.TimeManager.WORK_DAY)
                .lockScreenMode(ColumnDef.LockScreen.MODE_REST)
                .addUseRegion(regions)
                .addAppTypeConfig(ColumnDef.AppUseType.GAME_LOCK_TYPE, 46800)
                .build();
//
        LauncherManager.getInstance().addWorkMode(config);
        LauncherManager.getInstance().setCurrentWorkMode(ColumnDef.TimeManager.WORK_DAY);
//        handler.sendEmptyMessageDelayed(0, 15000);
    }


    private void toggleNotificationListenerService() {
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(this, NotificationListener2.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(new ComponentName(this, NotificationListener2.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }

    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }


    @Override
    protected void onResume() {
        super.onResume();
        Logger.e(TAG, PermissionUtil.hasOverLayerPermission(this) + "");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LauncherManager.getInstance().setInterceptSystemSetting(true);
        onResume();
    }



}
