package xn.hxp.view.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.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.room.bean.DeviceInfo;
import xn.hxp.room.tool.DeviceInfoTool;

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

    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);
        deviceInfo = DeviceInfoTool.INSTANCE.getDeviceInfo();
        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) {
                deviceInfo.setBleMac(deviceList.get(position).getAddress());
                deviceInfo.setBleName(deviceList.get(position).getName());
                DeviceInfoTool.INSTANCE.insert(deviceInfo);
                dismiss();
            }
        });
        binding.ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(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();
    }
}
