package com.xplora.xpsettings.Activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemProperties;
import android.widget.ListView;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Fragment.SelectFragment;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.Observer.Listener;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;

import java.util.Objects;

public class TestToolsActivity extends BaseActionActivity implements Listener.SelectFragmentListener {
    private static final String CIT_PACKAGE_NAME = "com.xplora.factorymode";
    private static final String CIT_ACTIVITY_NAME = "com.xplora.factorymode.activity.LaunchActicvity";
    private static final String RUNNING_ACTIVITY_NAME = "com.xplora.factorymode.modules.runin.RuninMainActivity";
    private static final String GPS_TEST_TOOL_NAME = "com.xplora.factorymode.modules.testtools.gpstool.GpsToolActivity";
    private static final String DEBUG_ON = "diag,serial_smd,rmnet_qti_bam,adb";
    private static final String DEBUG_OFF = "mtp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        setWatchTitle("TestTools");
        initData();
        initView();
    }

    private void initData() {
        String[] stringList = getResources().getStringArray(R.array.test_tool);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.titleTextSize = 14;
            model.value = "";
            model.isShowArrow = true;
            model.cellType = Constant.CellType.TITLE;
            if(i == 1) {
                String usbConfig = SystemProperties.get("sys.usb.config",  DEBUG_OFF);
                model.isOn = Objects.equals(usbConfig,  DEBUG_ON);
                model.cellType = Constant.CellType.SWITCH_RIGHT;
            } else if(i == 2) {
                model.isOn = Objects.equals(SystemProperties.get("persist.sys.offlinelog", "1"), "1");
                model.cellType = Constant.CellType.SWITCH_RIGHT;
            } else if (i == 5) {
                model.isOn = DataManager.getCloseDisableStatus() == 1;
                model.cellType = Constant.CellType.SWITCH_RIGHT;
            } else if (i == 6) {
                model.isOn = DataManager.getDonotShutdownStatus() == 1;
                model.cellType = Constant.CellType.SWITCH_RIGHT;
            } else if (i == 7) {
                model.isOn = DataManager.getVoLTE() == 1;
                model.cellType = Constant.CellType.SWITCH_RIGHT;
            }
            mDataList.add(model);
        }
    }

    private void initView() {
        BaseListAdapter adapter = new BaseListAdapter(TestToolsActivity.this, R.layout.item_title, mDataList, handler);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent;
            switch (i) {
                case 0:
                    intent=new Intent();
                    intent.setComponent(new ComponentName(CIT_PACKAGE_NAME, CIT_ACTIVITY_NAME));
                    startActivity(intent);
                    break;
                case 1:
                case 2:
                    // TODO: 2022/5/7 do nothing here
                    break;
                case 3:
                    String[] stringList = getResources().getStringArray(R.array.test_tool);
                    SelectFragment fragment = SelectFragment.newInstance(this);
                    fragment.setItemInfo(100, stringList[3], "confirm to start test");
                    fragment.show(this.getFragmentManager(), Constant.PACKAGE_NAME);
                    break;
                case 4:
                    intent=new Intent();
                    intent.setComponent(new ComponentName(CIT_PACKAGE_NAME, GPS_TEST_TOOL_NAME));
                    startActivity(intent);
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
                case 8:
                    Intent intent7 = new Intent(TestToolsActivity.this, AddWatchFaceActivity.class);
                    startActivity(intent7);
                    break;
                default:
                    break;
            }
        });
    }

    private void onOffLog(int isOn) {
        SystemProperties.set("persist.sys.offlinelog", String.valueOf(isOn));
    }

    private void onOffDebug(boolean isOn) {
        SystemProperties.set("sys.usb.config", isOn ? DEBUG_ON : DEBUG_OFF);
    }

    private void onOffDisable(boolean isOn) {
        DataManager.setCloseDisableStatus(isOn ? 1 : 0);
    }

    private void onOffShutdown(boolean isOn) {
        DataManager.setDonotShutdownStatus(isOn ? 1 : 0);
    }

    private void onOffVolte(boolean isOn) {
        DataManager.setVoLET(isOn ? 1 : 0);
    }

    @Override
    public void onSelectFragment(int type) {
        if (type == 100) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(CIT_PACKAGE_NAME, RUNNING_ACTIVITY_NAME));
            startActivity(intent);
        }
    }

    @Override
    public void onCancelFragment() {

    }

    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_SWITCH_CHANGE) {
                int position = msg.arg1; // 从Adapter获取当前item的位置
                int isOn = msg.arg2;     // 从Adapter获取开关状态（1=开，0=关）
                BaseModel model = mDataList.get(position);

                // 根据位置更新对应的数据状态
                switch (position) {
                    case 1:
                        onOffDebug(isOn == 1);
                        model.isOn = isOn == 1;
                        break;
                    case 2:
                        onOffLog(isOn);
                        model.isOn = isOn == 1;
                        break;
                    case 5:
                        onOffDisable(isOn == 1);
                        model.isOn = isOn == 1;
                        break;
                    case 6:
                        onOffShutdown(isOn == 1);
                        model.isOn = isOn == 1;
                        break;
                    case 7:
                        onOffVolte(isOn == 1);
                        model.isOn = isOn == 1;
                        break;
                }
            }
        }
    };
}
