package com.xplora.xpsettings.Activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.RelativeLayout;

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

import java.lang.reflect.Method;


public class SettingsActivity extends BaseActivity implements Listener.SelectFragmentListener {
    private boolean isLockClick = false; //防止快速多次点击不同item

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

    private void initData() {
        String[] stringList = ResUtils.getStringArray("settings_title_array_", 14);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.backgroundImage = ResUtils.getImageId("settings_bg_", i);
            model.title = stringList[i].trim();
            model.icon = ResUtils.getImageId("settings_icon_", i);
            model.cellType = Constant.CellType.XPSTYLE2;
            mDataList.add(model);
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(this, R.layout.item_xpstyle, mDataList);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(mAdapter);
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        //listView.setDividerHeight(4);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            if (isLockClick) {
                Log.d(TAG, "initView: listView click lock");
                return;
            }
            isLockClick = true;

            Intent intent;
            switch (i) {
                case 0:
                    intent = new Intent(this, ConnectionsActivity.class);
                    startActivity(intent);
                    break;
                case 1:
                    intent = new Intent(this, SoundModeActivity.class);
                    startActivity(intent);
                    break;
                case 2:
                    intent = new Intent(this, DisplayActivity.class);
                    startActivity(intent);
                    break;
                case 3:
                    intent = new Intent(this, BatteryActivity.class);
                    startActivity(intent);
                    break;
                case 4:
                    intent = new Intent(this, CameraActivity.class);
                    startActivity(intent);
                    break;
                case 5:
                    intent = new Intent(this, TimeFormatActivity.class);
                    startActivity(intent);
                    break;
                case 6:
                    intent = new Intent(this, LanguageActivity.class);
                    startActivity(intent);
                    break;
                case 7:
                    gotoQRcode();
                    break;
                case 8:
                    intent = new Intent(this, ApnListActivity.class);
                    startActivity(intent);
                    break;
                case 9:
                    gotoUpdate();
                    break;
                case 10:
                    //Monkey测试时，屏蔽关机
                    if (DataManager.getLockShutDown() == 1)
                        break;
                    SelectFragment fragment6 = SelectFragment.newInstance(this);
                    fragment6.setItemInfo(100, getString(R.string.restart), getString(R.string.confirm_to_restart));
                    fragment6.show(this.getFragmentManager(), Constant.PACKAGE_NAME);
                    break;
                case 11:
                    //Monkey测试时，屏蔽关机
                    if (DataManager.getLockShutDown() == 1)
                        break;
                    SelectFragment fragment7 = SelectFragment.newInstance(this);
                    fragment7.setItemInfo(101, getString(R.string.shut_down), getString(R.string.confirm_to_shut_down));
                    fragment7.show(this.getFragmentManager(), Constant.PACKAGE_NAME);
                    break;
                case 12:
                    intent = new Intent(this, AlertActivity.class);
                    startActivity(intent);
                    break;
                case 13:
                    intent = new Intent(this, AboutActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void onCancelFragment() {
        isLockClick = false;
    }

    @Override
    public void onSelectFragment(int type) {
        if (type == 100) {
            restart();
        } else if (type == 101) {
            shutdown();
        }

        //状态栏不显示
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //黑背景，防止关机中看到setting界面
        ShutdownningFragment fragment = ShutdownningFragment.newInstance(this);
        fragment.show(this.getFragmentManager(), Constant.PACKAGE_NAME);
    }

    public void gotoQRcode() {
        Log.d(TAG, "gotoQRcode: " + DataManager.getIsSupportESIM());
        if (DataManager.getIsSupportESIM()) {
            //该应用的包名
            String pkg = "com.gieseckedevrient.lpa.gsma.v22";
            //应用的主activity类
            String cls = pkg + ".euiccmanagement.EuiccManagementActivity";
            ComponentName componet = new ComponentName(pkg, cls);

            Intent intent = new Intent();
            intent.putExtra("intent_from", DataManager.isWatchActivated() >= 1 ? 3 : 1);
            intent.setComponent(componet);
            startActivity(intent);
        } else {
            //该应用的包名
            String pkg = "com.xplora.xponboarding";
            //应用的主activity类
            String cls = pkg + ".Activity.QRActivity";
            ComponentName componet = new ComponentName(pkg, cls);

            Intent intent = new Intent();
            intent.putExtra("intent_from", 3);
            intent.setComponent(componet);
            startActivity(intent);
        }
    }

    public void gotoUpdate() {
        //该应用的包名
        String pkg = "com.xplora.commonservice";
        //应用的主activity类
        String cls = pkg + ".ui.activity.OtaCheckActivity";
        ComponentName componet = new ComponentName(pkg, cls);

        Intent intent = new Intent();
        intent.putExtra("intent_from", 1);
        intent.setComponent(componet);
        startActivity(intent);
    }

    public void restart() {
        if (Macros.DEBUG_RESTART) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        PowerManager pManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
                        pManager.reboot("userrequested");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else {
            String pkg = "com.xplora.commonservice";
            String cls = pkg + ".modules.callbacks.LocalReceiver";
            ComponentName componet = new ComponentName(pkg, cls);

            Intent intent = new Intent("com.xplora.action.POWER_STATUS");
            intent.putExtra("type", 1);  //0:power off  1:reboot  2:factory reset
            intent.setComponent(componet);
            sendBroadcast(intent, "com.xplora.receiver");
            Log.d(TAG, "restart: ");
        }
    }

    public void shutdown() {
        if (Macros.DEBUG_RESTART) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        PowerManager pManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
                        if (pManager != null) {
                            Method method = pManager.getClass().getMethod("shutdown", boolean.class, String.class, boolean.class);
                            method.invoke(pManager, false, "userrequested", false);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else {
            String pkg = "com.xplora.commonservice";
            String cls = pkg + ".modules.callbacks.LocalReceiver";
            ComponentName componet = new ComponentName(pkg, cls);

            Intent intent = new Intent("com.xplora.action.POWER_STATUS");
            intent.putExtra("type", 0);  //0:power off  1:reboot  2:factory reset
            intent.setComponent(componet);
            sendBroadcast(intent, "com.xplora.receiver");
            Log.d(TAG, "shutdown: ");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        isLockClick = false;
    }
}
