package xn.hxp.weidith.ble;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.SPUtils;
import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleScanState;
import com.feasycom.common.bean.FscDevice;
import com.feasycom.spp.controler.FscSppCentralCallbacks;
import com.kongzue.dialogx.dialogs.PopTip;

import java.util.ArrayList;
import java.util.List;

import xn.hxp.databinding.DialogBleSelectorBinding;
import xn.hxp.utils.bluetooth.SppTool;

public class BleSelectorDialog extends AppCompatDialog {
    private Context context;
    private DialogBleSelectorBinding binding;
    private List<BluetoothDevice> deviceList = new ArrayList<>();
    private BleScanAdapter bleScanAdapter;

    public BleSelectorDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogBleSelectorBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        if (BleManager.getInstance().isBlueEnable()) {
            BleManager.getInstance().disconnectAllDevice();
        } else {
            BleManager.getInstance().enableBluetooth();
        }
        bleScanAdapter = new BleScanAdapter(context, deviceList);
        binding.bleLV.setAdapter(bleScanAdapter);
        binding.bleLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SPUtils.getInstance().put("sppMac", deviceList.get(position).getAddress());
                SPUtils.getInstance().put("sppName", deviceList.get(position).getName());
                dismiss();
            }
        });
        binding.ok.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                binding.ok.setVisibility(View.GONE);
                binding.cancel.setVisibility(View.VISIBLE);
                binding.bleLV.setVisibility(View.VISIBLE);
                binding.loadingLAV.setVisibility(View.VISIBLE);
                binding.loadingLAV.playAnimation();
                SppTool.INSTANCE.sacn(new FscSppCentralCallbacks() {
                    @Override
                    public void sppPeripheralFound(FscDevice fscDevice, int i) {
                        if (!TextUtils.isEmpty(fscDevice.getName())) {
                            deviceList.add(fscDevice.getDevice());
                        }
                        if (!deviceList.isEmpty()) {
                            bleScanAdapter.notifyDataSetChanged();
                            binding.loadingLAV.cancelAnimation();
                            binding.loadingLAV.setVisibility(View.GONE);
                        } else {
                            PopTip.show("未找到蓝牙称!");
                        }
                    }
                });
            }
        });
        binding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        binding.loadingLAV.cancelAnimation();
        if (BleManager.getInstance().getScanSate() == BleScanState.STATE_SCANNING) {
            BleManager.getInstance().cancelScan();
        }
    }
}
