package com.hh.arome.ui.setting;

import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

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.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 GatherActivity extends BaseHHActivity implements View.OnClickListener, DeviceConfigUpdateContract.View {

    TextView tv_title_center;
    ImageButton iv_title_left;
    private ImageView iv_zhifubao_face;
    private ImageView iv_idcard;
    private ImageView iv_idcard_online;
    private ImageView iv_idcard_offline;
    private ImageView iv_qr;
    private TextView tv_confirm;

    DeviceConfigUpdatePresenter mDeviceConfigUpdatePresenter;
    private Toolbar tb_center;
    private LinearLayout item_idcard_type;

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


    @Override
    protected void initView() {
        super.initView();

        tv_title_center = findViewById(R.id.tv_title_center);
        iv_title_left = findViewById(R.id.iv_title_left);

        iv_zhifubao_face = findViewById(R.id.iv_zhifubao_face);
        iv_idcard = findViewById(R.id.iv_idcard);
        iv_idcard_online = findViewById(R.id.iv_idcard_online);
        iv_idcard_offline = findViewById(R.id.iv_idcard_offline);
        iv_qr = findViewById(R.id.iv_qr);
        tv_confirm = findViewById(R.id.tv_confirm);
        iv_title_left.setOnClickListener(this);
        iv_zhifubao_face.setOnClickListener(this);
        iv_idcard.setOnClickListener(this);
        iv_idcard_online.setOnClickListener(this);
        iv_idcard_offline.setOnClickListener(this);
        iv_qr.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        tb_center = findViewById(R.id.tb_center);
        item_idcard_type = findViewById(R.id.item_idcard_type);
    }


    DeviceConfigResultBean deviceConfigBean;

    boolean isUseAlipayFace;
    boolean isUseIdcard;
    boolean isUseQrcode;

    int idNumberReaderType = 1;

    boolean isIdcardOnline;
    boolean isIdcardOffline;

    @Override
    protected void initData() {

        tv_title_center.setText("采集配置");

        mDeviceConfigUpdatePresenter = new DeviceConfigUpdatePresenter(this);

        deviceConfigBean = ConfigData.getDeviceConfigResultBean();

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

        isUseAlipayFace = deviceConfigBean.isUseAlipayFace();
        isUseIdcard = deviceConfigBean.isUseIdcard();
        isUseQrcode = deviceConfigBean.isUseQrcode();


        setSwitchView(iv_zhifubao_face, isUseAlipayFace);
        setSwitchView(iv_idcard, isUseIdcard);
        setSwitchView(iv_qr, isUseQrcode);

        setIdcardTypeView();

        idNumberReaderType = deviceConfigBean.getIdNumberReaderType();
        if (idNumberReaderType == 1) {
            setCheckView(iv_idcard_online, true);
            setCheckView(iv_idcard_offline, false);

            isIdcardOnline = true;
            isIdcardOffline = false;

        } else if (idNumberReaderType == 2) {
            setCheckView(iv_idcard_online, false);
            setCheckView(iv_idcard_offline, true);

            isIdcardOnline = false;
            isIdcardOffline = true;
        }


    }

    private void setIdcardTypeView() {
        if (isUseIdcard) {
            item_idcard_type.setVisibility(View.VISIBLE);
        } else {
            item_idcard_type.setVisibility(View.GONE);
        }
    }


    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);
        }
    }

    public void setCheckView(ImageView iv, boolean flag) {
        if (flag) {
            UIUtils.setImageResource(iv, R.mipmap.ic_setting_switch_on);
        } else {
            UIUtils.setImageResource(iv, R.mipmap.ic_setting_switch_off);
        }
    }


    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.iv_zhifubao_face:
                isUseAlipayFace = !isUseAlipayFace;

                setSwitchView(iv_zhifubao_face, isUseAlipayFace);
                break;
            case R.id.iv_idcard:
                isUseIdcard = !isUseIdcard;

                setSwitchView(iv_idcard, isUseIdcard);

                setIdcardTypeView();

                break;
            case R.id.iv_idcard_online:

//                isIdcardOnline = !isIdcardOnline;
//                setCheckView(iv_idcard_online, isIdcardOnline);

                if (isIdcardOnline) {

                } else {
                    isIdcardOnline = true;
                }
                setCheckView(iv_idcard_online, true);
                setCheckView(iv_idcard_offline, false);

                break;
            case R.id.iv_idcard_offline:

//                isIdcardOffline = !isIdcardOffline;
//                setCheckView(iv_idcard_offline, isIdcardOffline);

                if (isIdcardOffline) {

                } else {
                    isIdcardOffline = true;
                }
                setCheckView(iv_idcard_online, false);
                setCheckView(iv_idcard_offline, true);

                break;
            case R.id.iv_qr:
                isUseQrcode = !isUseQrcode;
                setSwitchView(iv_qr, isUseQrcode);

                break;
            case R.id.tv_confirm:

                handleDataAndRequest();

                break;
        }
    }

    private void handleDataAndRequest() {

        deviceConfigBean.setUseAlipayFace(isUseAlipayFace);
        deviceConfigBean.setUseIdcard(isUseIdcard);
        deviceConfigBean.setUseQrcode(isUseQrcode);

        if (isIdcardOffline) {
            deviceConfigBean.setIdNumberReaderType(1);
        } else {
            deviceConfigBean.setIdNumberReaderType(2);
        }

//        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) {

        ToastUtils.showLongToast(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()
        );



        delayFinish();
    }
}
