package com.ruansee.macall.testwebview.activity;

import androidx.annotation.Nullable;

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.app.RuanseeApp;
import com.ruansee.macall.testwebview.databinding.ActivityMainBinding;

public class MainActivity extends BaseViewBindActivity<ActivityMainBinding> {

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

    @Override
    protected void init() {

    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

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

    @Override
    protected void start() {
        setInitData();
    }

    private void setInitData() {
        AppCacheData appCacheData = RuanseeApp.getAppCacheData();
        if (appCacheData == null){
            appCacheData = AppCacheData.getCacheData();
        }
        if (!appCacheData.isUserEffective()){
            ToastUtil.centered(getContext(),"无效用户，请先设置用户新！");
            onSkipSetting(null);
            return;
        }
        viewBinding.mainUserName.setText(String.format("%s(%s)",appCacheData.getUserName(),appCacheData.getDeptCode()));
        viewBinding.mainDeptName.setText(StringUtil.get(appCacheData.getDeptName(),appCacheData.getDeptCode()));
        String idCardNum = appCacheData.getIdCardNum();
        if (StringUtil.isNullOrEmpty(idCardNum)){
            viewBinding.mainUserSfzh.setText("暂无");
        }else {
            char[] chars = idCardNum.toCharArray();
            //3424221993 0   6  0  4  1  4 1  3
            //0123456789 10  11 12 13 14 15
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i <chars.length; i++) {
                if (i>= 8 && i<=14){
                    builder.append("*");
                    continue;
                }
                builder.append(chars[i]);
            }
            viewBinding.mainUserSfzh.setText(builder.toString());
        }
        viewBinding.mainAppName.setText(appCacheData.getAppName());
    }

    public void onStartTest(View view){
        AppCacheData appCacheData = RuanseeApp.getAppCacheData();
        Intent intent = new Intent(getContext(), OpenAppActivity.class);
        intent.putExtra(OpenAppActivity.paramKey1, appCacheData.getClientId());
        intent.putExtra(OpenAppActivity.paramKey2, appCacheData.getClientSecret());
        intent.putExtra(OpenAppActivity.paramKey3, appCacheData.getAppUrl());
        intent.putExtra(OpenAppActivity.paramKey9, appCacheData.getAppName());
        intent.putExtra(OpenAppActivity.paramKey10, "管理员");
        intent.putExtra(OpenAppActivity.paramKey11, "18888686866");
        startActivity(intent);
    }

    public void onSkipSetting(View view){
        Intent intent = new Intent(getContext(), SettingActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        if (resultCode == 5151){
            //重新加载用户信息和应用信息
            setInitData();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}