package com.hh.arome.activity.setting;



import static com.hh.lib_base.Constants.LogcatValue.LINE;
import static com.hh.lib_base.Constants.LogcatValue.NODE;
import static com.hh.lib_base.Constants.LogcatValue.WARP;

import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;


import com.hh.arome.R;

import com.hh.arome.bean.SimpleResultBean;
import com.hh.arome.presenter.setting.SystemConfigPresenter;
import com.hh.arome.utils.UIUtils;
import com.hh.lib_base.ConfigData;
import com.hh.lib_base.Constants;
import com.hh.lib_base.activity.BaseActivity;
import com.hh.lib_base.activity.BaseMVPActivity;
import com.hh.lib_base.bean.device.DeviceConfigResultBean;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_base.log.LogType;
import com.hh.lib_base.utils.DoubClickUtils;
import com.hh.lib_base.utils.ImageLoaderUtils;
import com.hh.lib_base.utils.StringUtils;
import com.hh.lib_base.utils.ToastUtils;
import com.hh.lib_base.utils.device.DeviceUtils;
import com.hh.lib_device.deviceui.BaseDeviceUI;
import com.hh.lib_device.deviceui.IDeviceUI;


public class SystemConfigActivity extends BaseMVPActivity<SystemConfigPresenter> implements View.OnClickListener {

    private ImageButton iv_title_left;
    private TextView tv_title_center;
    private Toolbar tb_center;
    private EditText et_pwd;
    private ImageView iv_navigation_bar;
    private ImageView iv_notification_pull_down;
    private ImageView iv_enter_applet;
    private ImageView iv_splash;
    private ImageView iv_main;
    private TextView tv_confirm;
    private ImageView iv_app_auto_start;

    String deviceModel = DeviceUtils.getDeviceModel();
    DeviceConfigResultBean deviceConfigBean;
    boolean forbidPullDown;
    boolean hideNavigationBar;
    boolean automaticEnterApplet;
    boolean autoLaunch;

    IDeviceUI iDeviceUI;

    @NonNull
    @Override
    public SystemConfigPresenter initPresenter() {
        return new SystemConfigPresenter();
    }

    protected int getLayoutId() {
        if(Constants.DeviceModel.D2.equals(deviceModel)) {
            return R.layout.activity_system_config_hp;
        }else{
            return R.layout.activity_system_config;
        }
    }


    @Override
    protected void initView() {
        iv_title_left = findViewById(R.id.iv_title_left);
        tv_title_center = findViewById(R.id.tv_title_center);
        tb_center = findViewById(R.id.tb_center);
        et_pwd = findViewById(R.id.et_pwd);
        iv_navigation_bar = findViewById(R.id.iv_navigation_bar);
        iv_notification_pull_down = findViewById(R.id.iv_notification_pull_down);
        iv_enter_applet = findViewById(R.id.iv_enter_applet);
        iv_splash = findViewById(R.id.iv_splash);
        iv_main = findViewById(R.id.iv_main);
        tv_confirm = findViewById(R.id.tv_confirm);
        iv_title_left.setOnClickListener(this);
        iv_navigation_bar.setOnClickListener(this);
        iv_notification_pull_down.setOnClickListener(this);
        iv_enter_applet.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        iv_app_auto_start = findViewById(R.id.iv_app_auto_start);
        iv_app_auto_start.setOnClickListener(this);
    }




    @Override
    protected void initData() {
        tv_title_center.setText("系统配置");

        iDeviceUI = BaseDeviceUI.getInstance().getDeviceUI();


        deviceConfigBean = ConfigData.getDeviceConfigResultBean();

        if (deviceConfigBean == null) {
            AppLogUtils.e("deviceConfigBean == null");
            deviceConfigBean = new DeviceConfigResultBean();
        }

        UIUtils.setEditText(et_pwd, deviceConfigBean.getMpassword());


        // 禁止下拉通知栏 false-不启用(默认) true-启用
        forbidPullDown = deviceConfigBean.isForbidPullDown();
        AppLogUtils.e("forbidPullDown = " + forbidPullDown);

        // 隐藏底部导航栏 false-不启用(默认) true-启用
        hideNavigationBar = deviceConfigBean.isHideNavigationBar();
        AppLogUtils.e("hideNavigationBar = " + hideNavigationBar);

        // 是否自动进入小程序 false-不启用(默认) true-启用
        automaticEnterApplet = deviceConfigBean.isAutomaticEnterApplet();
        AppLogUtils.e("automaticEnterApplet = " + automaticEnterApplet);

        // 自动进入应用
        autoLaunch = deviceConfigBean.isAutoLaunch();
        AppLogUtils.e("autoLaunch = " + autoLaunch);

        setSwitchView(iv_notification_pull_down, forbidPullDown);
        setSwitchView(iv_navigation_bar, hideNavigationBar);
        setSwitchView(iv_enter_applet, automaticEnterApplet);
        setSwitchView(iv_app_auto_start, autoLaunch);

        ImageLoaderUtils.Companion.loadImage(deviceConfigBean.getStartPageUrl(), iv_splash);
        ImageLoaderUtils.Companion.loadImage(deviceConfigBean.getHomePageUrl(), iv_main);


    }

    private void setSwitchView(ImageView iv, boolean flag) {
        if (flag) {
            UIUtils.setImageResource(iv, R.mipmap.iv_swich_open);
        } else {
            UIUtils.setImageResource(iv, R.mipmap.iv_swich_close);
        }
    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void onClick(View var1) {
        switch (var1.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.iv_navigation_bar:
                hideNavigationBar = !hideNavigationBar;

                setSwitchView(iv_navigation_bar, hideNavigationBar);

                break;
            case R.id.iv_notification_pull_down:

                forbidPullDown = !forbidPullDown;


                setSwitchView(iv_notification_pull_down, forbidPullDown);

                break;
            case R.id.iv_enter_applet:

                automaticEnterApplet = !automaticEnterApplet;

                setSwitchView(iv_enter_applet, automaticEnterApplet);

                break;
            case R.id.iv_app_auto_start:

                autoLaunch = !autoLaunch;

                setSwitchView(iv_app_auto_start, autoLaunch);

                break;
            case R.id.tv_confirm:

                handleDataAndRequest();
                break;

        }
    }

    private void handleDataAndRequest() {
        String pwdStr = UIUtils.getEditText(et_pwd);
        if (StringUtils.isNull(pwdStr)) {
            ToastUtils.showLongToast("密码不能为空");
            return;
        }

        deviceConfigBean.setMpassword(pwdStr);

        if (forbidPullDown) {
            iDeviceUI.hideNotification(SystemConfigActivity.this);
        } else {
            iDeviceUI.showNotification(SystemConfigActivity.this);
        }

        if (hideNavigationBar) {
            iDeviceUI.hideNavigationBar(SystemConfigActivity.this);
        } else {
            iDeviceUI.showNavigationBar(SystemConfigActivity.this);
        }


        deviceConfigBean.setHideNavigationBar(hideNavigationBar);
        deviceConfigBean.setForbidPullDown(forbidPullDown);
        deviceConfigBean.setAutomaticEnterApplet(automaticEnterApplet);
        deviceConfigBean.setAutoLaunch(autoLaunch);


        ToastUtils.showLongToast("本地配置已生效");
        ConfigData.setDeviceConfigResultBean(deviceConfigBean);

        // 开始请求接口
        mPresenter.requestDeviceConfigUpdate(deviceConfigBean);
    }

    public void showDeviceConfigUpdateData(SimpleResultBean resultBean) {
        ToastUtils.showLongToast("在线配置已更新");
        delayFinish();

    }


    public void showError(String url, String msg) {

        // 开始保存日志
        AppLogUtils.saveLog(LogType.Logcat, new StringBuilder()
                .append(LINE)
                .append((new Throwable().getStackTrace())[0]).append(WARP)
                .append(NODE)
                .append("网络请求结果异常").append(WARP)
                .append("url = ").append(url).append(WARP)
                .append("msg = ").append(msg)
                .toString()
        );

        ToastUtils.showLongToast(msg + "");

        delayFinish();

    }

    public void delayFinish(){
        new Handler().postDelayed(new Runnable() {
            public void run() {
                finish();
            }
        }, 1500);
    }


}
