package aicare.net.cn.sdk.ailinksdkdemoandroid;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.pingwang.bluetoothlib.AILinkSDK;
import com.pingwang.bluetoothlib.bean.BleHandshakeBean;
import com.pingwang.bluetoothlib.bean.BleScanAddAilinkBean;
import com.pingwang.bluetoothlib.bean.BleValueBean;
import com.pingwang.bluetoothlib.config.BleConfig;
import com.pingwang.bluetoothlib.device.BleDevice;
import com.pingwang.bluetoothlib.listener.OnCallbackBle;
import com.pingwang.bluetoothlib.listener.OnScanFilterListener;
import com.pingwang.bluetoothlib.utils.BleLog;
import com.pingwang.bluetoothlib.utils.BleStrUtils;

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

import aicare.net.cn.sdk.ailinksdkdemoandroid.adapter.StringAdapter;
import aicare.net.cn.sdk.ailinksdkdemoandroid.base.BleBaseActivity;
import aicare.net.cn.sdk.ailinksdkdemoandroid.config.BleDeviceConfig;
import aicare.net.cn.sdk.ailinksdkdemoandroid.dialog.HintDataDialogFragment;
import aicare.net.cn.sdk.ailinksdkdemoandroid.dialog.LoadingIosDialogFragment;
import aicare.net.cn.sdk.ailinksdkdemoandroid.home.HomeDataManager;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.CheckPermissionUtils;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.MyBleStrUtils;
import aicare.net.cn.sdk.ailinksdkdemoandroid.view.MyItemDecoration;
import cn.net.aicare.modulelibrary.module.RopeSkipping.RopeSkippingBleData;
import cn.net.aicare.modulelibrary.module.scooter.SkateboardBleConfig;


/**
 * xing<br>
 * 2019/3/6<br>
 * 扫描蓝牙设备界面
 */
public class ShowBleActivity extends BleBaseActivity implements OnCallbackBle, OnScanFilterListener {

    public static final int REQUEST_PERMISSION_CODE = 1500;

    private static String TAG = ShowBleActivity.class.getName();

    private final int BIND_SERVER_OK = 1;
    private final int BIND_SERVER_ERR = 2;
    private final int REFRESH_DATA = 3;
    private EditText et_cid;
    private EditText et_filter_name;
    private List<BleValueBean> mBleValueList;
    private StringAdapter listAdapter;

    private Context mContext;
    private int mType;
    private String mNoEncryptionMac = "";
    private String mFilterName = "";
    private String mFilterMac = "";
    private int mCid;
    private int mVid;
    private int mPid;
    private int mScanCid = 0;
    private String mBleName;
    private Class<?> targetClass;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case BIND_SERVER_OK:

                    break;

                case REFRESH_DATA:
                    if (listAdapter != null) {
                        listAdapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_ble);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle(getString(R.string.app_name) + BuildConfig.VERSION_NAME);
        }
        mContext = this;
        mType = getIntent().getIntExtra("type", 0);
        mCid = getIntent().getIntExtra("cid", 0);
        if (0 == mType) {
            finish();
            return;
        }
        mScanCid = mCid;
        targetClass = HomeDataManager.getTargetActivityClass(mType);
        if (targetClass == null) {
            return;
        }
        init();
        initData();
        if (mCid == BleDeviceConfig.TOOTHBRUSH_TEST) {
            // 如果是蓝牙牙刷，就弹个框提醒
            new AlertDialog.Builder(mContext).setMessage("启动牙刷，工作10秒钟，然后关闭牙刷（让牙刷处于广播状态），然后点击搜索，选中列表中的牙刷，开始测试")
                    .setPositiveButton("确认", null)
                    .show();
        }
    }


    private void initData() {

    }


    private void init() {

        mBleValueList = new ArrayList<>();
        RecyclerView recyclerView = findViewById(R.id.rv_data);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new MyItemDecoration(mContext, LinearLayoutManager.VERTICAL, 1, mContext.getResources()
                .getColor(R.color.public_press_bg)));
        Button btn = findViewById(R.id.btn);
        Button btn1 = findViewById(R.id.btn1);
        Button clear = findViewById(R.id.clear);
        et_filter_name = findViewById(R.id.et_filter_name);
        EditText et_filter_mac = findViewById(R.id.et_filter_mac);
        et_cid = findViewById(R.id.et_cid);
        if (mScanCid > 0) {
            et_cid.setText(MyBleStrUtils.getHexString(mScanCid));
        } else {
            et_cid.setText(String.valueOf(mScanCid));
        }


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAILinkBleManager != null) {
                    BleLog.i(TAG, "搜索设备");
                    try {
                        String cidStr = et_cid.getText().toString().trim();
                        mScanCid = Integer.parseInt(cidStr, 16);
                    } catch (NumberFormatException e) {
                        mScanCid = -1;
                        e.printStackTrace();
                    }
                    mFilterName = et_filter_name.getText().toString().trim();
                    mFilterMac = et_filter_mac.getText().toString().trim();
                    long scanOut = 30 * 1000;
                    if (mType == BleDeviceConfig.SMART_SCOOTER) {
                        List<BleScanAddAilinkBean> list = new ArrayList<>();
                        list.add(new BleScanAddAilinkBean(0x37, 0x03, 0x01, SkateboardBleConfig.UUID_BROADCAST));
                        mAILinkBleManager.startScan(scanOut, list, BleConfig.UUID_SERVER_AILINK, SkateboardBleConfig.UUID_BROADCAST);
                    } else if (mType == BleDeviceConfig.BLE_SMART_CYCLING_TABLE) {
                        mAILinkBleManager.startScan(scanOut);
                    } else {
//                        0000FEE7=手表
                        mAILinkBleManager.startScan(scanOut, BleConfig.UUID_BROADCAST_AILINK, BleConfig.UUID_SERVER_BROADCAST_AILINK, UUID.fromString("0000FEE7-0000-1000-8000-00805F9B34FB"), SkateboardBleConfig.UUID_BROADCAST);
//                        BleScanUtil.scanLeDevice(AILinkBleManager.getInstance(), scanOut,BleConfig.UUID_BROADCAST_AILINK, BleConfig.UUID_SERVER_BROADCAST_AILINK);
                    }
                    mBleValueList.clear();
                    listAdapter.notifyDataSetChanged();
                }
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAILinkBleManager != null) {
                    mAILinkBleManager.stopScan();
                }
            }
        });
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAILinkBleManager != null) {
                    mBleValueList.clear();
                    listAdapter.notifyDataSetChanged();
                }
            }
        });

        listAdapter = new StringAdapter(this, mBleValueList, new StringAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                BleValueBean bleValueBean = mBleValueList.get(position);
                String mac = bleValueBean.getMac();
                mCid = bleValueBean.getCid();
                mVid = bleValueBean.getVid();
                mPid = bleValueBean.getPid();
                mBleName = bleValueBean.getName();

//                if (mType == BleDeviceConfig.CLEAR_SHAKE_HANDS || mType == BleDeviceConfig.BLE_SEND_SPEED_TEST) {
                if (mType == BleDeviceConfig.CLEAR_SHAKE_HANDS) {
                    //验证不握手,不加密的界面使用
                    //设置不握手
                    BleConfig.setHandshakeStatus(mac, false);
                    if (mAILinkBleManager != null) {
                        mAILinkBleManager.stopScan();
                        mAILinkBleManager.connectDevice(new BleValueBean(mac, mCid, mVid, mPid));
                        showLoading();
                    }
                } else {
                    if (mCid == BleDeviceConfig.BLE_BOOLD_OXYGEN && mVid == 0x0012) {
                        //vid=12的不用握手校验,不加密
                        mNoEncryptionMac = mac;
                        BleConfig.setHandshakeStatus(mac, false);
                    } else if (mCid == BleDeviceConfig.SMART_SCOOTER) {
                        if (mPid == 0x0001) {
                            //电滑板pid=01的不用握手校验,不加密
                            BleConfig.setHandshakeStatus(mac, false);
                        }
                        mNoEncryptionMac = mac;
                    } else if (mCid == 0) {
                        //CID=0不需要握手
                        BleConfig.setHandshakeStatus(mac, false);
                    } else if (mCid == BleDeviceConfig.ROPE_SKIPPING) {
                        //要加密要握手。别人家的东西
                        if (mVid == 0x0027 && mPid == 0x0001) {
                            BleConfig.setHandshakeStatus(RopeSkippingBleData.LongXiang, mac, true);
                        } else if (mVid == 0x0003 && mPid == 0x0009) {
                            //设置A7不加密
                            mNoEncryptionMac = mac;
                        }
                    } else if (mCid == BleDeviceConfig.MEAT_PROBE_CHARGER) {
                        //探针充电盒握手
                        if (mVid == 0x54) {
                            BleConfig.setHandshakeStatus(mac, true, mCid, mVid, mPid);
                            mNoEncryptionMac = mac;
                        }
                    }
                    if (mAILinkBleManager != null) {
                        mAILinkBleManager.stopScan();
                        mAILinkBleManager.connectDevice(new BleValueBean(mac, mCid, mVid, mPid));
                        showLoading();
                    }
                }
            }

            @Override
            public void onLongClick(int position) {
                BleValueBean bleValueBean = mBleValueList.get(position);
                if (bleValueBean == null) {
                    return;
                }
                StringBuilder showData = new StringBuilder();
                if (bleValueBean.getParcelUuids() != null) {
                    showData.append("UUID:");
                    for (ParcelUuid parcelUuid : bleValueBean.getParcelUuids()) {
                        showData.append(parcelUuid.toString().substring(0, 8)).append(";");
                    }
                    showData.append("\n");
                }
                String data2 = BleStrUtils.byte2HexStr(bleValueBean.getManufacturerData());
                showData.append(data2);
                HintDataDialogFragment.newInstance()
                        .setTitle("自定义厂商数据", 0)
                        .setContent(showData.toString(), false)
                        .setOk("", 0)
                        .show(getSupportFragmentManager());
            }
        });
        recyclerView.setAdapter(listAdapter);


        // 延迟后请求权限
        btn.postDelayed(() -> {
            if (isDestroyed()) {
                return;
            }

            checkPermission();
        }, 500);
    }


    //---------------------------------服务---------------------------------------------------


    @Override
    public void onStartScan() {

    }

    private long mOldRefreshTime = 0;

    @Override
    public void onScanning(@NonNull BleValueBean data) {
        String mAddress = data.getMac();
        BleLog.i(TAG, "MAC=" + mAddress + "||CID=" + data.getCid() + "||VID=" + data.getVid() + "||PID=" + data.getPid());

        boolean oldData = false;
        for (int i = 0; i < mBleValueList.size(); i++) {
            BleValueBean bleValueBean = mBleValueList.get(i);
            if (bleValueBean.equals(data)) {
                bleValueBean.setRssi(data.getRssi());
                oldData = true;
                listAdapter.notifyItemChanged(i);
                break;
            }
        }
        if (!oldData) {
            //不是旧设备,是新的,添加
            mBleValueList.add(data);
            listAdapter.notifyDataSetChanged();
        } else {
            //            if (System.currentTimeMillis()-mOldRefreshTime>500){
            //                mOldRefreshTime=System.currentTimeMillis();
            //                listAdapter.notifyDataSetChanged();
            //            }
        }
        //        String data1 = BleStrUtils.byte2HexStr(data.getScanRecord());
        //        String data2 = BleStrUtils.byte2HexStr(data.getManufacturerData());
        //        BleLog.i(TAG, "设备地址+广播数据:" + mAddress + "||" + data1 + "||" + data2);


    }


    @Override
    public void onConnecting(@NonNull String mac) {

    }

    @Override
    public void onDisConnected(@NonNull String mac, int code) {
        dismissLoading();
        Toast.makeText(mContext, "连接断开:" + code, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onServicesDiscovered(@NonNull String mac) {
        BleDevice bleDevice = mAILinkBleManager.getBleDevice(mac);
        initEncryption(bleDevice);
        if (mac.equalsIgnoreCase(mNoEncryptionMac)) {
            bleDevice.setA7Encryption(false);
            mNoEncryptionMac = "";

        }
        dismissLoading();
        Intent intent = new Intent();
        int type = mType;//默认婴儿秤
        intent.setClass(ShowBleActivity.this, targetClass);
        mAILinkBleManager.removeOnCallbackBle(this);
        intent.putExtra("type", type);
        intent.putExtra("mac", mac);
        intent.putExtra("cid", mCid);
        intent.putExtra("vid", mVid);
        intent.putExtra("pid", mPid);
        intent.putExtra("bleName", mBleName);
        startActivity(intent);

    }


    @Override
    public void bleOpen() {

    }

    @Override
    public void bleClose() {
        checkPermission();
    }

    @Override
    public boolean onFilter(BleValueBean bleValueBean) {
        int cid = bleValueBean.getCid();
        boolean nameAndMac = false;
        if (TextUtils.isEmpty(mFilterName) && TextUtils.isEmpty(mFilterMac)) {
            nameAndMac = true;
        }
        if (!TextUtils.isEmpty(mFilterName)) {
            String name = bleValueBean.getName();
            if (name != null && name.toUpperCase().contains(mFilterName.toUpperCase())) {
                nameAndMac = true;
            }
        }
        if (!TextUtils.isEmpty(mFilterMac)) {
            String mac = bleValueBean.getMac().replace(":", "").toUpperCase().trim();
            nameAndMac = mac.contains(mFilterMac.toUpperCase());
        }

        if (mScanCid == BleDeviceConfig.TOOTHBRUSH_TEST && nameAndMac) {
            return cid == BleDeviceConfig.TOOTHBRUSH_TEST || cid == BleDeviceConfig.TOOTHBRUSH_WIFI_BLE;
        }
        if (mScanCid == BleDeviceConfig.TEMP_Humidity && nameAndMac) {
            return cid == BleDeviceConfig.TEMP_Humidity || cid == BleDeviceConfig.TEMP_Humidity_wifi;
        }
        if (mScanCid == BleDeviceConfig.FOOD_TEMP && nameAndMac) {
            return cid == BleDeviceConfig.FOOD_TEMP || cid == 63;
        }
//        if (mScanCid == BleDeviceConfig.BLOOD_PRESSURE && nameAndMac) {
//            return cid == BleDeviceConfig.BLOOD_PRESSURE || cid == BleDeviceConfig.SPHY_WIFI_BLE;
//        }
        if (mScanCid < 0) {
            return nameAndMac;
        } else {
            return isCheckDevice(mScanCid, Math.abs(cid), nameAndMac);
        }

    }


    private boolean isCheckDevice(int scanCid, int cid, boolean nameAndMac) {
        boolean okDevice = scanCid == cid && nameAndMac;
        return okDevice;
    }


    @Override
    public void onScanTimeOut() {

    }

    @Override
    public void onScanErr(int type, long time) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            checkPermission();
        }
    }


    @Override
    public void onServiceSuccess() {
        if (mAILinkBleManager != null) {
            mAILinkBleManager.setOnCallbackBle(ShowBleActivity.this);
            mAILinkBleManager.setOnScanFilterListener(ShowBleActivity.this);
//            mAILinkBleManager.initForegroundService(1, R.mipmap.ic_launcher, "前台服务", MainActivity.class);
//            mAILinkBleManager.startForegroundService();//启动前台服务
            mHandler.sendEmptyMessage(BIND_SERVER_OK);
        }
    }

    @Override
    public void onServiceErr() {

    }

    @Override
    public void unbindServices() {
        if (mAILinkBleManager != null) {
            mAILinkBleManager.removeOnCallbackBle(this);
        }
    }

    //--------------------------start Loading--------------------------
    private LoadingIosDialogFragment mDialogFragment;

    /**
     * 显示加载
     */
    private void showLoading() {
        if (mDialogFragment == null) {
            mDialogFragment = new LoadingIosDialogFragment();
        }
        mDialogFragment.show(getSupportFragmentManager());
    }

    /**
     * 关闭加载
     */
    private void dismissLoading() {
        if (mDialogFragment != null) {
            mDialogFragment.dismiss();
        }
    }

    //--------------------------end Loading--------------------------


    @Override
    protected void onResume() {
        super.onResume();
        if (mAILinkBleManager != null) {
            mAILinkBleManager.setOnCallbackBle(ShowBleActivity.this);
            mAILinkBleManager.setOnScanFilterListener(ShowBleActivity.this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAILinkBleManager != null) {
            mAILinkBleManager.stopScan();
        }
    }


    // ------------------- 权限 ------------------

    /**
     * 检查权限
     */
    private void checkPermission() {
        new CheckPermissionUtils(this).checkPermissions(new CheckPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionsOk() {
                ShowBleActivity.this.onPermissionsOk();
            }
        });
        // 没有蓝牙权限就请求蓝牙权限
        if (!hasBluetooth()) {
            requestBluetooth();
            return;
        }
        // 没有定位权限就请求定位权限
        if (!hasLocationPermission()) {
            requestLocationPermission(this);
            return;
        }
        // 没有定位服务就请求定位服务
        if (!hasLocationService()) {
            requestLocationService();
            return;
        }
        // 都有了，OK
        Toast.makeText(mContext, "权限都有，可以开始搜索", Toast.LENGTH_SHORT).show();
    }

    /**
     * 权限ok
     */
    protected void onPermissionsOk() {

    }

    /**
     * 是否有定位权限
     *
     * @return boolean
     */
    private boolean hasLocationPermission() {
        return ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 蓝牙是否打开
     *
     * @return boolean
     */
    private boolean hasBluetooth() {
        return BluetoothAdapter.getDefaultAdapter().isEnabled();
    }

    /**
     * 定位服务是否打开
     *
     * @return boolean
     */
    private boolean hasLocationService() {
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null) {
            return false;
        }
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

    /**
     * 申请定位权限
     */
    private void requestLocationPermission(Activity activity) {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_CODE);
    }

    /**
     * 申请打开蓝牙
     */
    @SuppressLint("MissingPermission")
    private void requestBluetooth() {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, REQUEST_PERMISSION_CODE);
    }

    /**
     * 申请打开定位服务
     */
    private void requestLocationService() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivityForResult(intent, REQUEST_PERMISSION_CODE);
    }

    /**
     * 初始化加密
     * 主要用于license设备,不用加密
     */
    public static void initEncryption(BleDevice bleDevice) {
        if (bleDevice == null) {
            return;
        }
        BleValueBean bleValueBean = bleDevice.getBleValueBean();
        if (bleValueBean != null) {
            BleHandshakeBean licenseDevice = AILinkSDK.getInstance().isLicenseDevice(bleValueBean.getCid(), bleValueBean.getVid(), bleValueBean.getPid());
            if (licenseDevice != null) {
                //license设备,不用加密
                bleDevice.setA7Encryption(false);
            }
        }
    }
}
