package com.xplora.xpsettings.Activity;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.WindowManager;

import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Fragment.ShutdownningFragment;
import com.xplora.xpsettings.Observer.Listener;
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 BaseActionActivity extends BaseActivity implements Listener.SelectFragmentListener {
    public int mActionType = 0;

    public enum ACTION_TYPE {
        AIRPLANE_MODE,
        VIBRATE,
        VOLTE,
        MOBILE_DATA,
        DATA_ROAMING,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionType = getIntent().getIntExtra(Constant.INTENT_ACTION_TYPE, 0);
        Log.d(TAG, "onCreate: " + mActionType);
    }

    public String getActionTitle() {
        if (mActionType == ACTION_TYPE.AIRPLANE_MODE.ordinal()) {
            String[] stringList = ResUtils.getStringArray("sk_settings_title_array_", 5);
            return stringList[0];
        } else if (mActionType == ACTION_TYPE.VIBRATE.ordinal()) {
            String[] stringList = ResUtils.getStringArray("soundmode_title_array_", 2);
            return stringList[1];
        } else if (mActionType == ACTION_TYPE.VOLTE.ordinal()) {
            String[] stringList = ResUtils.getStringArray("telephone_title_array_", 3);
            return stringList[0];
        } else if (mActionType == ACTION_TYPE.MOBILE_DATA.ordinal()) {
            String[] stringList = ResUtils.getStringArray("telephone_title_array_", 3);
            return stringList[1];
        } else if (mActionType == ACTION_TYPE.DATA_ROAMING.ordinal()) {
            String[] stringList = ResUtils.getStringArray("telephone_title_array_", 3);
            return stringList[2];
        }
        return "";
    }

    public int getActionStatus() {
        return getActionStatus(mActionType);
    }

    public int getActionStatus(int type) {
        Log.d(TAG, "getActionStatus: " + type);
        if (type == ACTION_TYPE.AIRPLANE_MODE.ordinal()) {
            return DataManager.getAirplaneMode();
        } else if (type == ACTION_TYPE.VIBRATE.ordinal()) {
            return DataManager.getIsVibrate();
        } else if (type == ACTION_TYPE.VOLTE.ordinal()) {
            return DataManager.getVoLTE();
        } else if (type == ACTION_TYPE.MOBILE_DATA.ordinal()) {
            return DataManager.isMobileDataEnabled() ? 1: 0;
        } else if (type == ACTION_TYPE.DATA_ROAMING.ordinal()) {
            return DataManager.getDataRoamingType();
        }
        return 0;
    }

    public void setActionStatus(int status) {
        setActionStatus(mActionType, status);
    }

    public void setActionStatus(int type, int status) {
        Log.d(TAG, "setActionStatus: " + type + " status:" + status);
        if (type == ACTION_TYPE.AIRPLANE_MODE.ordinal()) {
            changeAirplaneMode(status);
        } else if (type == ACTION_TYPE.VIBRATE.ordinal()) {
            DataManager.setIsVibrate(status);
        } else if (type == ACTION_TYPE.VOLTE.ordinal()) {
            DataManager.setVoLET(status);
        } else if (type == ACTION_TYPE.MOBILE_DATA.ordinal()) {
            DataManager.setMobileDataEnabled(status == 1);
        } else if (type == ACTION_TYPE.DATA_ROAMING.ordinal()) {
            DataManager.setDataRoamingType(status);
        }
    }

    public void changeAirplaneMode(int status) {
        // 设置飞行模式状态
        DataManager.setAirplaneMode(status);

        // 发送广播通知系统飞行模式状态已改变
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", status == 1);
        sendBroadcast(intent);
    }

    public void gotoQRcode() {
        Log.d(TAG, "gotoQRcode: " + DataManager.getIsSupportESIM());
        if (DataManager.getIsSupportESIM()) {
            String pkg = "com.gieseckedevrient.lpa.gsma.v22";
            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";
            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 gotoPws() {
        String pkg = "com.sikey.cellbroadcastreceiver";
        String cls = pkg + ".CellBroadcastListActivity";
        ComponentName componet = new ComponentName(pkg, cls);
        Intent intent = new Intent();
        intent.setComponent(componet);
        startActivity(intent);
    }

    public void gotoUpdate() {
        String pkg = "com.sikey.commonservice";
        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.sikey.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.sikey.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: ");
        }
    }

    public void restoreFactorySettings() {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        devicePolicyManager.wipeData(0);
    }

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

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

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

    @Override
    public void onCancelFragment() {

    }
}
