package com.android.launcher3.settings;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.android.launcher3.R;
import com.secspace.lib.common.utils.ToastUtil;
import com.secspace.mdmengine.api.manager.MdmFactoryManager;

public class ManagerDisableActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private TextView mTvTitle;
    private TextView mTvFinish;
    private Switch mSwitchOtg;
    private Switch mSwitchUsb;
    private Switch mSwitchAdb;
    private Switch mSwitchSD;

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

    private void initView() {
        mTvFinish = findViewById(R.id.tv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mSwitchAdb = findViewById(R.id.switch_adb);
        mSwitchOtg = findViewById(R.id.switch_otg);
        mSwitchUsb = findViewById(R.id.switch_usb);
        mSwitchSD = findViewById(R.id.switch_sd);
        mTvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        boolean usbDataDisabled = MdmFactoryManager.getInstance().getDeviceRestrictionManager().isUSBDataDisabled();
        boolean adbDisabled = MdmFactoryManager.getInstance().getDeviceRestrictionManager().isAdbDisabled();
        boolean usbOtgDisabled = MdmFactoryManager.getInstance().getDeviceRestrictionManager().isUSBOtgDisabled();
        boolean externalStorageDisabled = MdmFactoryManager.getInstance().getDeviceRestrictionManager().isExternalStorageDisabled();
        mSwitchOtg.setChecked(!usbOtgDisabled);
        mSwitchAdb.setChecked(!adbDisabled);
        mSwitchUsb.setChecked(!usbDataDisabled);
        mSwitchSD.setChecked(!externalStorageDisabled);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText("功能管控");
        mSwitchUsb.setOnCheckedChangeListener(this);
        mSwitchOtg.setOnCheckedChangeListener(this);
        mSwitchAdb.setOnCheckedChangeListener(this);
        mSwitchSD.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.isPressed()) {
            switch (buttonView.getId()) {
                case R.id.switch_usb:
                    MdmFactoryManager.getInstance().getDeviceRestrictionManager().setUSBDataDisabled(!isChecked);
                    if (!isChecked) {
                        MdmFactoryManager.getInstance().getDeviceRestrictionManager().setAdbDisabled(true);
                        mSwitchAdb.setChecked(false);
                    }
                    break;
                case R.id.switch_otg:
                    MdmFactoryManager.getInstance().getDeviceRestrictionManager().setUSBOtgDisabled(!isChecked);
                    break;
                case R.id.switch_adb:
                    boolean usbDataDisabled = MdmFactoryManager.getInstance().getDeviceRestrictionManager().isUSBDataDisabled();
                    if (usbDataDisabled && isChecked) {
                        mSwitchAdb.setChecked(false);
                        ToastUtil.showShort(this, "USB存储被禁用，请先打开USB存储");
                        return;
                    }
                    MdmFactoryManager.getInstance().getDeviceRestrictionManager().setAdbDisabled(!isChecked);
                    break;
                case R.id.switch_sd:
                    MdmFactoryManager.getInstance().getDeviceRestrictionManager().setExternalStorageDisabled(!isChecked);
                    break;
            }
        }
    }
}
