package com.android.launcher3.debug;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.launcher3.BaseActivity;
import com.android.launcher3.R;
import com.android.launcher3.debug.adapter.TimeAdapter;
import com.android.launcher3.debug.adapter.WorkModeAdapter;
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.util.Logger;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 显示管控信息
 */
public class DebugInfoActivity extends BaseActivity implements View.OnClickListener {

    ImageView iv_back;

    Button btn_change_exit_state;
    TextView tv_exit_status;

    Button btn_intercept_system_settings;
    TextView tv_intercept_system_settings;

    TextView tv_current_work_mode;
    RecyclerView rv_work_mode;

    RecyclerView rv_app_manage_info;

    private WorkModeAdapter workModeAdapter;

    private TimeAdapter timeAdapter;

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

        iv_back = findViewById(R.id.iv_back);
        btn_change_exit_state = findViewById(R.id.btn_change_exit_state);
        tv_exit_status = findViewById(R.id.tv_exit_status);
        btn_intercept_system_settings = findViewById(R.id.btn_intercept_system_settings);
        tv_intercept_system_settings = findViewById(R.id.tv_intercept_system_settings);
        tv_current_work_mode = findViewById(R.id.tv_current_work_mode);

        rv_work_mode = findViewById(R.id.rv_work_mode);
        rv_app_manage_info = findViewById(R.id.rv_app_manage_info);


        iv_back.setOnClickListener(this);
        btn_change_exit_state.setOnClickListener(this);
        btn_intercept_system_settings.setOnClickListener(this);

        workModeAdapter = new WorkModeAdapter();

        rv_work_mode.setLayoutManager(new LinearLayoutManager(this));
        rv_work_mode.setAdapter(workModeAdapter);

        timeAdapter = new TimeAdapter();
        rv_app_manage_info.setLayoutManager(new LinearLayoutManager(this));
        rv_app_manage_info.setAdapter(timeAdapter);

        //TimeModeManager.getInstance(getApplicationContext()).setCurrentWorkModel(ColumnDef.TimeManager.WEEK_RESET);
        //LauncherManager.getInstance().lockScreenNormal(getApplicationContext());

        loadWorkMode();
        loadAppInfo();
        update();
    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.btn_change_exit_state) {
            LauncherManager.getInstance().changeExitState(!LauncherManager.getInstance().getExitState());
            update();
        } else if (id == R.id.btn_intercept_system_settings) {
            LauncherManager.getInstance().setInterceptSystemSetting(!LauncherManager.getInstance().isInterceptSystemSetting());
            update();
        }
    }


    private void update() {
        if (LauncherManager.getInstance().getExitState()) {
            btn_change_exit_state.setText("进入管制");
            tv_exit_status.setText("当前未进入管制状态，所有规则均不生效");
            tv_exit_status.setTextColor(getResources().getColor(android.R.color.holo_red_dark));

        } else {
            btn_change_exit_state.setText("退出管制");
            tv_exit_status.setText("当前已进入管制状态，所有规则已生效");
            tv_exit_status.setTextColor(getResources().getColor(android.R.color.black));
        }


        if (LauncherManager.getInstance().isInterceptSystemSetting()) {
            btn_intercept_system_settings.setText("关闭拦截系统设置");
            tv_intercept_system_settings.setText("当前已开启拦截系统设置");
            tv_intercept_system_settings.setTextColor(getResources().getColor(android.R.color.black));

        } else {
            btn_intercept_system_settings.setText("开启拦截系统设置");
            tv_intercept_system_settings.setText("当前已关闭拦截系统设置");
            tv_intercept_system_settings.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
        }
    }


    public void loadWorkMode() {
        switch (TimeModeManager.getInstance(getApplicationContext()).getCurrentWorkModel()) {
            case ColumnDef.TimeManager.HOLIDAY_MODE:
                tv_current_work_mode.setText("当前模式：假期模式");
                break;
            case ColumnDef.TimeManager.WEEK_RESET:
                tv_current_work_mode.setText("当前模式：周末模式");
                break;
            case ColumnDef.TimeManager.WORK_DAY:
                tv_current_work_mode.setText("当前模式：工作日模式");
                break;
            default:
                tv_current_work_mode.setText("当前模式：未设置模式");
        }

        if (TimeModeManager.getInstance(getApplicationContext()).getConfigMap() != null) {
            Iterator<Map.Entry<Integer, TimeConfig>> iterator = TimeModeManager.getInstance(getApplicationContext()).getConfigMap().entrySet().iterator();

            List<TimeConfig> timeConfigs = new ArrayList<>();
            while (iterator.hasNext()) {
                Map.Entry<Integer, TimeConfig> entry = iterator.next();
                timeConfigs.add(entry.getValue());
            }
            workModeAdapter.setTimeConfigs(timeConfigs);
            workModeAdapter.notifyDataSetChanged();
        }

    }


    public void loadAppInfo() {
        Logger.e("loadAppInfo", "loadAppInfo\t" + LauncherManager.getInstance().getAppInfo().size());
        if (LauncherManager.getInstance().getAppInfo() != null) {
            Iterator<Map.Entry<String, ManagementInfo>> iterator = LauncherManager.getInstance().getAppInfo().entrySet().iterator();

            List<ManagementInfo> timeConfigs = new ArrayList<>();
            while (iterator.hasNext()) {
                Map.Entry<String, ManagementInfo> entry = iterator.next();
                timeConfigs.add(entry.getValue());
            }
            timeAdapter.setManagementInfos(timeConfigs);
            timeAdapter.notifyDataSetChanged();
        }

    }
}
