package com.rdiot.yx485.device.view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import com.rdiot.yx485.R;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class BindDeviceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_device);
    }
    private void startRingAnimation() {
        RotateAnimation animation = new RotateAnimation(
                0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
        );
        animation.setDuration(1300);
        animation.setRepeatCount(-1);
        animation.setFillAfter(true);
        animation.setInterpolator(new LinearInterpolator());
//        binding.ivRing.startAnimation(animation);
    }
    private void startBindSubDevice() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            CompletableFuture.runAsync(() -> {
                try {
                  //  addRoom(bindViewModel.getRoomName().getValue(), bindViewModel.getPanelNum().getValue(), bindViewModel.getDeviceTypeId().getValue()).await();
                    TimeUnit.MILLISECONDS.sleep(1500);
                    bindSuccess();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    private void startBindDevice() {
//        BleCtrl.autoBind(this,
//                bindViewModel.getPanelNum().getValue(),
//                bindViewModel.getRoomName().getValue(),
//                bindViewModel.getWifiSsid().getValue(),
//                bindViewModel.getWifiPwd().getValue(),
//                bindViewModel.getDeviceTypeId().getValue(),
//                e -> {
//                    if (!isResumed()) return;
//
//                    String evenString = "Step:" + e.getStep() + " -- Code:" + e.getCode();
//                    LogUtils.i(evenString);
//                    binding.tvDebug.setText(binding.tvDebug.getText() + evenString + "\n");
//
//                    if (bindViewModel.getCode() == DeviceCode.INFRARED.typeCode) {
//                        bindViewModel.getPanelNum().setValue(e.getId());
//                    }
//
//                    if (e.getStep() == 5 && e.getCode() == 0) {
//                        bindSuccess();
//                    }
//
//                    if (!e.isSuccess()) {
//                        showErrEventDialog(e.getCode(), null);
//                        BleCtrl.cancelBind();
//                        bindViewModel.stopCountDown();
//                        bindViewModel.stopCheckOnline();
//                    } else {
//                        binding.tvSearch.setText(R.string.device_adding);
//                    }
//                });
//
//        getLifecycle().addObserver(BleCtrl);
    }

    private void bindSuccess() {
//        bindViewModel.stopCountDown();
//        bindViewModel.stopCheckOnline();
//        if (!bindViewModel.isBindSub()) {
//            //更新定位信息
//            LocalData.getFamilyData().getValue().setCity(bindViewModel.getLocationName());
//            CompletableFuture.runAsync(() -> {
//                try {
//                    updateFamilyData(LocalData.getFamilyData().getValue()).await();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//
//        if (binding.btnNext.getVisibility() != View.VISIBLE) {
//            loadOneTimeGif(requireActivity(), R.mipmap.finish, binding.ivFinish);
//            binding.btnNext.setVisibility(View.VISIBLE);
//            binding.tvDebug.setText(getString(R.string.add_finish));
//            BleCtrl.cancelBind();
//        }
//
//        if (bindViewModel.getCode() == DeviceCode.INFRARED.typeCode) {
//            CompletableFuture.runAsync(() -> {
//                try {
//                    addMasterRoom(bindViewModel.getRoomName().getValue(), bindViewModel.getPanelNum().getValue(), bindViewModel.getDeviceTypeId().getValue()).await();
//                    TimeUnit.MILLISECONDS.sleep(1500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//
//        // 更新设备类型
//        CompletableFuture.runAsync(() -> {
//            try {
//                updateRoomDeviceType(bindViewModel.getPanelNum().getValue(), bindViewModel.getDeviceTypeId().getValue()).await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
//
//        setClickLimitListener(binding.btnNext, v -> requireActivity().finish());
    }

}