package com.hh.arome.ui.setting;

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.appcompat.widget.Toolbar;

import com.common.lib_base.utils.GlideUtils;
import com.common.lib_base.utils.StringUtils;
import com.common.lib_base.utils.ui.ToastUtils;
import com.common.lib_base.utils.ui.UIUtils;
import com.hh.arome.R;
import com.hh.arome.bean.DeviceConfigResultBean;
import com.hh.arome.bean.SimpleResultBean;
import com.hh.arome.common.ConfigData;
import com.hh.arome.common.deviceui.BaseDeviceUI;
import com.hh.arome.common.deviceui.IDeviceUI;
import com.hh.arome.mvp.contract.DeviceConfigUpdateContract;
import com.hh.arome.mvp.presenter.DeviceConfigUpdatePresenter;
import com.hh.arome.ui.base.BaseHHActivity;
import com.hh.arome.utlis.log.AppLogUtils;
import com.hh.arome.utlis.log.LogType;

import static com.hh.arome.common.Constants.LogcatValue.LINE;
import static com.hh.arome.common.Constants.LogcatValue.NODE;
import static com.hh.arome.common.Constants.LogcatValue.WARP;

public class SystemConfigActivity extends BaseHHActivity implements View.OnClickListener, DeviceConfigUpdateContract.View {

    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;

    DeviceConfigUpdatePresenter mDeviceConfigUpdatePresenter;

    @Override
    public int setLayout() {
        return R.layout.activity_system_config;
    }

    @Override
    protected void initView() {
        super.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);
    }


    DeviceConfigResultBean deviceConfigBean;


    boolean forbidPullDown;
    boolean hideNavigationBar;
    boolean automaticEnterApplet;

    IDeviceUI iDeviceUI;

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

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

        mDeviceConfigUpdatePresenter = new DeviceConfigUpdatePresenter(this);

        deviceConfigBean = ConfigData.getDeviceConfigResultBean();

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

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


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

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

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


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


        GlideUtils.loadImageView(mActivity, deviceConfigBean.getStartPageUrl(), iv_splash);
        GlideUtils.loadImageView(mActivity, 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 initListener() {

    }


    @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.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(mActivity);
        } else {
            iDeviceUI.showNotification(mActivity);
        }

        if (hideNavigationBar) {
            iDeviceUI.hideNavigationBar(mActivity);
        } else {
            iDeviceUI.showNavigationBar(mActivity);
        }

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


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

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

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

        delayFinish();

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    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();

    }
}
