package xn.hxp.ui.start;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

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.WaitDialog;

import xn.hxp.databinding.ActivitySettingBinding;
import xn.hxp.room.bean.DeviceInfo;
import xn.hxp.room.tool.DeviceInfoTool;
import xn.hxp.util.Tool;
import xn.hxp.view.ble.BleSelectorDialog;
import xn.hxp.view.ble.SppTool;

public class SettingActivity extends AppCompatActivity {

    private ActivitySettingBinding binding;
    private DeviceInfo deviceInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        deviceInfo = DeviceInfoTool.INSTANCE.getDeviceInfo();

        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(AppUtils.isAppDebug() ? "http://192.168.1.8" : "http://172.16.0.65");
        binding.bleET.setText(TextUtils.isEmpty(deviceInfo.getBleMac()) ? "" : deviceInfo.getBleMac());
        binding.bleNameTV.setText(TextUtils.isEmpty(deviceInfo.getBleName()) ? "" : deviceInfo.getBleName());

        if (deviceInfo.isFrontCamera()) {
            // 默认前置相机
            binding.cameraRG.check(binding.frontRB.getId());
        } else {
            // 后置相机
            binding.cameraRG.check(binding.backRB.getId());
        }
        // 管理员密码
        binding.oldPasswordET.setText(TextUtils.isEmpty(deviceInfo.getAdminPwd()) ? "hxp" : deviceInfo.getAdminPwd());
        // 域名配置
        if (!TextUtils.isEmpty(deviceInfo.getBaseUrl())) {
            binding.httpUriET.setText(deviceInfo.getBaseUrl());
        }
        // 测试地址输入
        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 -> {
                DeviceInfo sqlInfo = DeviceInfoTool.INSTANCE.getDeviceInfo();
                binding.bleET.setText(TextUtils.isEmpty(sqlInfo.getBleMac()) ? "" : sqlInfo.getBleMac());
                binding.bleNameTV.setText(TextUtils.isEmpty(sqlInfo.getBleName()) ? "" : sqlInfo.getBleName());
                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.bleET.setError("请配置蓝牙称设备");
                return;
            }

            deviceInfo.setBleMac(bleTVText.toString());
            deviceInfo.setFrontCamera(binding.frontRB.isChecked());
            deviceInfo.setAdminPwd(oldPasswordETText.toString());
            deviceInfo.setBaseUrl(httpUriETText.toString());
            WaitDialog.show("稍后重启App");
            DeviceInfoTool.INSTANCE.insert(deviceInfo);
            binding.backRB.postDelayed(() -> AppUtils.relaunchApp(true), 1500);
        } catch (Exception e) {
            ToastUtils.showLong("参数异常无法保存,请核对参数!");
            LogUtils.e(Log.getStackTraceString(e));
        }
    }
}