package com.hh.arome.activity.setting;

import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
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.FunctionPresenter;
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.utils.StringUtils;
import com.hh.lib_base.utils.ToastUtils;
import com.hh.lib_base.utils.device.DeviceUtils;
import com.hh.lib_common.local.AppLocalConfigHandler;
import com.hh.lib_common.local.IAppLocalConfig;

public class FunctionActivity extends BaseMVPActivity<FunctionPresenter> implements View.OnClickListener {


    private ImageButton iv_title_left;
    private TextView tv_title_center;
    private Toolbar tb_center;
    private EditText et_timeout;
    private TextView tv_confirm;


    String deviceModel = DeviceUtils.getDeviceModel();

    DeviceConfigResultBean deviceConfigBean;

    IAppLocalConfig localConfig;

    @Override
    protected int getLayoutId() {
        if(Constants.DeviceModel.D2.equals(deviceModel)) {
            return R.layout.activity_setting_function_hp;
        }else{
            return R.layout.activity_setting_function;
        }
    }

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

    @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_timeout = findViewById(R.id.et_timeout);
        tv_confirm = findViewById(R.id.tv_confirm);
        iv_title_left.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
    }



    @Override
    protected void initData() {

        tv_title_center.setText("功能配置");



        localConfig = AppLocalConfigHandler.create(FunctionActivity.this.getApplicationContext(), IAppLocalConfig.class);

        int appletOperationTimeout = localConfig.getAppletOperationTimeout();
        UIUtils.setEditText(et_timeout,appletOperationTimeout+"");


        deviceConfigBean = ConfigData.getDeviceConfigResultBean();

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

    }



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

                handleDataAndRequest();

                break;
        }
    }


    private void handleDataAndRequest() {


        String timeStr = UIUtils.getEditText(et_timeout);
        if(StringUtils.isNull(timeStr)){
            ToastUtils.showLongToast("时间不能为空，或点击左上角返回");
            return;
        }else{
            int i = Integer.parseInt(timeStr);
            if(i > 600){
                ToastUtils.showLongToast("不能大于600秒");
                return;
            }
            localConfig.setAppletOperationTimeout(i);
        }

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

        delayFinish();

//        deviceConfigBean.setUseAlipayFace(isUseAlipayFace);


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

    }


    public void showDeviceConfigUpdateData(SimpleResultBean resultBean) {

    }




    public void showError(String url, String msg) {

    }



    @Override
    protected void initEvent() {

    }

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