package com.xplora.xpsettings.Activity;

import static com.xplora.xpsettings.Activity.WiFiActivity.MSG_SWITCH_CHANGE;

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.util.Log;
import android.widget.ListView;

import com.xplora.xpsettings.Adapter.ListAdapter;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TestToolsActivity extends BaseActivity {
    private static final String CIT_PACKAGE_NAME = "com.zeusis.factorymode";
    private static final String CIT_ACTIVITY_NAME = "com.zeusis.factorymode.activity.LaunchActicvity";
    private static final String RUNNING_ACTIVITY_NAME = "com.zeusis.factorymode.modules.runin.RuninMainActivity";
    private static final String GPS_TEST_TOOL_NAME = "com.zeusis.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";

    public static final int MSG_LOGGABLE_CHANGE = 0;
    public static final int MSG_DEBUGABLE_CHANGE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        setWatchTitle(getString(R.string.title_testtools));

        initData();
        initView();
    }

    private void initData() {
        String[] stringList = getResources().getStringArray(R.array.test_tool);
        for (String s : stringList) {
            BaseModel model = new BaseModel();
            model.title = s.trim();
            model.value = "";
            model.isShowArrow = true;
            if(model.title.equals("loggable")) {
                //model.isOn = Objects.equals(SystemProperties.get("persist.sys.offlinelog", "1"), "1");
                model.isOn = Objects.equals("1", "1");
            }
            if(model.title.equals("Debugable")) {
//                String usbConfig = SystemProperties.get("sys.usb.config",  DEBUG_OFF);
                String usbConfig = "1";
                model.isOn = Objects.equals(usbConfig,  DEBUG_ON);
            }
            mDataList.add(model);
        }
    }

    private void initView() {
        ListAdapter adapter = new ListAdapter(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:
                    intent=new Intent();
                    intent.setComponent(new ComponentName(CIT_PACKAGE_NAME, RUNNING_ACTIVITY_NAME));
                    startActivity(intent);
                    break;
                case 4:
                    intent=new Intent();
                    intent.setComponent(new ComponentName(CIT_PACKAGE_NAME, GPS_TEST_TOOL_NAME));
                    startActivity(intent);
                    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);
    }

    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MSG_LOGGABLE_CHANGE) {
                onOffLog((int) msg.obj);
            } else if (msg.what == MSG_DEBUGABLE_CHANGE) {
                onOffDebug((boolean) msg.obj);
            }
        }
    };
}
