package xn.hxp.ui;

import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;

import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.rc.core.ui.activity.BaseActivity;

import xn.hxp.databinding.ActivitySettingBinding;
import xn.hxp.utils.SharedPreferencesHelper;
import xn.hxp.utils.Tool;
import xn.hxp.utils.bluetooth.SppTool;
import xn.hxp.weidith.ble.BleSelectorDialog;

public class SettingActivity extends BaseActivity {

    private ActivitySettingBinding binding;

    @Override
    protected ViewBinding setViewBinding() {
        return binding = ActivitySettingBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onInit() {
        binding.varBT.setText("版本:" + AppUtils.getAppVersionName());
        binding.snBT.setText("SN:" + Tool.INSTANCE.getSerialNumber());
        binding.ipBT.setText(NetworkUtils.getIPAddress(true));
        binding.rebootBT.setOnClickListener(v -> Tool.INSTANCE.cmd("reboot"));
        binding.fileBrowserBT.setOnClickListener(v -> Tool.INSTANCE.openFileBrowser());
        binding.settingBT.setOnClickListener(v -> Tool.INSTANCE.openSetting());
        binding.barSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> Tool.INSTANCE.showBar(isChecked));
        binding.httpUriET.setText(Tool.INSTANCE.getBaseUrl().toString());
        binding.bleET.setText(SPUtils.getInstance().getString("sppMac", ""));
        binding.bleNameTV.setText(SPUtils.getInstance().getString("sppName", ""));
        // 前同事代码
        SettingsBean settingsBean = SharedPreferencesHelper.INSTANCE.getUrlBase(this);
        if (null != settingsBean) {
            String cameraSelector = settingsBean.getD();
            // 后置相机
            if ("1".equals(cameraSelector)) {
                binding.cameraRG.check(binding.backRB.getId());
            }
            // 默认前置相机
            else {
                binding.cameraRG.check(binding.frontRB.getId());
            }
            // 管理员密码
            binding.oldPasswordET.setText(TextUtils.isEmpty(settingsBean.getPwd()) ? "admin@098&" : settingsBean.getPwd());
            // 域名配置
            String baseApi = settingsBean.getA();
            if (!TextUtils.isEmpty(baseApi)) {
                binding.httpUriET.setText(baseApi);
            }
        } else {
            if (AppUtils.isAppDebug()) {
                binding.httpUriET.setText("http://192.168.1.8/api/");
            } else {
                binding.httpUriET.setText("http://172.16.0.65/api/");
            }

            // 管理员密码
            binding.oldPasswordET.setText("admin@098&");
        }
        // 测试地址输入
        binding.httpUriTestBT.setOnClickListener(v -> binding.httpUriET.setText("http://192.168.1.8/api/"));
        // 线上地址输入
        binding.httpUriXnBT.setOnClickListener(v -> binding.httpUriET.setText("http://172.16.0.65/api/"));
        // 蓝牙扫描
        binding.scanBT.setOnClickListener(v -> {
            BleSelectorDialog bleSelectorDialog = new BleSelectorDialog(SettingActivity.this);
            bleSelectorDialog.setOnDismissListener(dialog -> {
                binding.bleET.setText(SPUtils.getInstance().getString("sppMac", ""));
                binding.bleNameTV.setText(SPUtils.getInstance().getString("sppName", ""));
                SppTool.INSTANCE.disconnect();
            });
            bleSelectorDialog.show();
        });
        // 保存
        binding.save.setOnClickListener(v -> save());

    }

    @Override
    public void onBackPressed() {
    }

    private void save() {
        try {
            Editable httpUriETText = binding.httpUriET.getText();
            if (null == httpUriETText || TextUtils.isEmpty(httpUriETText)) {
                binding.httpUriET.setError("请输入Http服务地址");
                return;
            }
            Editable oldPasswordETText = binding.oldPasswordET.getText();
            if (null == oldPasswordETText || TextUtils.isEmpty(oldPasswordETText)) {
                binding.oldPasswordET.setError("请输入管理员密码");
                return;
            }
            CharSequence bleTVText = binding.bleET.getText();
            if (null == bleTVText || TextUtils.isEmpty(bleTVText)) {
                binding.oldPasswordET.setError("请配置蓝牙称设备");
                return;
            }
            SPUtils.getInstance().put("sppMac", bleTVText.toString());
            SPUtils.getInstance().put("cameraSelector", binding.backRB.isChecked() ? 1 : 0);
            SharedPreferencesHelper.INSTANCE.saveUrlBase(this, new SettingsBean(httpUriETText.toString(), httpUriETText.toString(), httpUriETText.toString(), binding.frontRB.isChecked() ? "0" : "1", oldPasswordETText.toString()));
            WaitDialog.show("稍后重启App");
            binding.backRB.postDelayed(() -> AppUtils.relaunchApp(true), 1500);
        } catch (Exception e) {
            ToastUtils.showLong("参数异常无法保存,请核对参数!");
            LogUtils.e(Log.getStackTraceString(e));
        }
    }

    @Override
    protected void cdFinish() {

    }

    @Override
    protected void cdTime(int cd) {

    }
}