package com.ruansee.macall.testwebview.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.ruansee.macall.testwebview.app.AppCacheData;
import com.ruansee.macall.testwebview.R;
import com.ruansee.macall.testwebview.app.RuanseeApp;
import com.ruansee.macall.testwebview.databinding.ActivitySettingBinding;

public class SettingActivity extends BaseViewBindActivity<ActivitySettingBinding> {
    private boolean isSkipMain = false;

    @Override
    protected ActivitySettingBinding getViewBinding() {
        return ActivitySettingBinding.inflate(getLayoutInflater());
    }

    @Override
    protected boolean isImmersiveStatusBar() {
        return true;
    }

    @Override
    protected void init() {

    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {
        isSkipMain = getIntent().getBooleanExtra("isSkipMain", false);
    }

    @Override
    protected void start() {
        viewBinding.include.contentTitle.setText("设置");
        viewBinding.include.leftImage.setImageDrawable(getDrawables(R.drawable.back_cliener));
        viewBinding.include.titleleft.setOnClickListener(v -> finish());
        setData();
    }

    private void setData() {
        AppCacheData appCacheData = RuanseeApp.getAppCacheData();
        if (appCacheData == null) {
            appCacheData = AppCacheData.getCacheData();
        }

        viewBinding.settingAppName.setText(appCacheData.getAppName());
        viewBinding.settingAppUrl.setText(appCacheData.getAppUrl());
        viewBinding.settingUserCode.setText(appCacheData.getUserCode());
        viewBinding.settingUserName.setText(appCacheData.getUserName());
        viewBinding.settingUserSfzh.setText(appCacheData.getIdCardNum());
        viewBinding.settingDeptCode.setText(appCacheData.getDeptCode());
        viewBinding.settingDeptName.setText(appCacheData.getDeptName());
        viewBinding.settingAppClientId.setText(appCacheData.getClientId());
        viewBinding.settingAppClientSecret.setText(appCacheData.getClientSecret());

    }

    public void onSubmit(View view) {
        String appName = getEditText(viewBinding.settingAppName);
        String appUrl = getEditText(viewBinding.settingAppUrl);
        String userCode = getEditText(viewBinding.settingUserCode);
        String userName = getEditText(viewBinding.settingUserName);
        String idCardName = getEditText(viewBinding.settingUserSfzh);
        String deptCode = getEditText(viewBinding.settingDeptCode);
        String deptName = getEditText(viewBinding.settingDeptName);
        String clientId = getEditText(viewBinding.settingAppClientId);
        String clientSecret = getEditText(viewBinding.settingAppClientSecret);
        if (judge(userCode, "请输入用户警号")) return;
        if (judge(userName, "请输入用户姓名")) return;
        if (judge(idCardName, "请输入用户证件号码")) return;
        if (judge(deptCode, "请输入用户机构代码")) return;
        if (judge(appName, "请输入应用名称")) return;
        if (judge(appUrl, "请输入应用地址")) return;
        if (judge(clientId, "请输入clientId")) return;
        if (judge(clientSecret, "请输入clientSecret")) return;
        AppCacheData appCacheData = RuanseeApp.getAppCacheData();
        appCacheData.setAppName(appName);
        appCacheData.setAppUrl(appUrl);
        appCacheData.setDeptCode(deptCode);
        appCacheData.setDeptName(deptName);
        appCacheData.setIdCardNum(idCardName);
        appCacheData.setUserCode(userCode);
        appCacheData.setUserName(userName);
        appCacheData.setClientId(clientId);
        appCacheData.setClientSecret(clientSecret);
        if (isSkipMain) {
            startActivity(new Intent(getContext(), MainActivity.class));
        }else {
            setResult(5151);
        }
        finish();
    }

    private boolean judge(String value, String titMsg) {
        if (StringUtil.isNullOrEmpty(value)) {
            ToastUtil.centered(getContext(), titMsg);
            return true;
        }
        return false;
    }

}