package com.wisewoods.base.camera;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.luck.picture.lib.camera.listener.ClickListener;
import com.wisewoods.base.R;
import com.wisewoods.base.app.BaseApp;
import com.wisewoods.base.bean.Camera;
import com.wisewoods.base.bean.Results;
import com.wisewoods.base.bean.UserToken;
import com.wisewoods.base.databinding.ViewRoomCameraBinding;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.utils.$;
import com.wisewoods.base.utils.DialogUtils;

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

import retrofit2.Response;

public class RoomCameraDialog extends BottomSheetDialog {


    private static HashMap<Integer, String> map = new HashMap<>();

    static {
        map.put(R.id.fl_top_left, "4");
        map.put(R.id.fl_top, "0");
        map.put(R.id.fl_top_right, "6");
        map.put(R.id.fl_left, "2");
        map.put(R.id.fl_right, "3");
        map.put(R.id.fl_bottom_left, "5");
        map.put(R.id.fl_bottom, "1");
        map.put(R.id.fl_bottom_right, "7");
        map.put(R.id.fl_narrow, "9");
        map.put(R.id.fl_enlarge, "8");

        //    {number: 10, label: '近焦'},
//    {number: 11, label: '远焦'},],
    }

    private ViewRoomCameraBinding binding;
    ClickListener listener;
    private String access_token;
    private int isComplete;
    private ArrayList<Camera> cameras;
    private String code;
    private String[] cId;

    public void setListener(ClickListener listener) {
        this.listener = listener;
    }


    public RoomCameraDialog(String entity, String[] cId) {
        super(ActivityUtils.getTopActivity(), R.style.BottomSheetDialog);

        this.cId = cId;
        binding = ViewRoomCameraBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        cameras = new ArrayList<>();
        binding.tvDevice.setOnClickListener(v -> {
            if (cId.length == 1) {
                return;
            }
            if (isComplete == cId.length) {
                List<CharSequence> al = new ArrayList<>();
                for (Camera camera : cameras) {
                    al.add(camera.data.cameraName);
                }
                DialogUtils.showList("请选择摄像头", al, (materialDialog, index, text) -> {
                    code = cameras.get(index).data.cameraCode;
                    binding.tvDevice.setText(entity + cameras.get(index).data.cameraName + "摄像头");
                    return null;
                });
            }

        });
        binding.tvDevice.setText(entity);

        binding.flTopLeft.setOnClickListener(this::cmd);
        binding.flTop.setOnClickListener(this::cmd);
        binding.flTopRight.setOnClickListener(this::cmd);
        binding.flLeft.setOnClickListener(this::cmd);
        binding.flRight.setOnClickListener(this::cmd);
        binding.flBottomLeft.setOnClickListener(this::cmd);
        binding.flBottom.setOnClickListener(this::cmd);
        binding.flBottomRight.setOnClickListener(this::cmd);
        binding.flNarrow.setOnClickListener(this::cmd);
        binding.flEnlarge.setOnClickListener(this::cmd);

        BaseApp.api.getCameraToken()
                .enqueue(new JCallback<UserToken>() {
                    @Override
                    public void onS(Response<UserToken> response, UserToken userToken) {
                        access_token = userToken.data.access_token;
                        for (String s : cId) {
                            BaseApp.api.getCamera(s)
                                    .enqueue(new JCallback<Camera>() {
                                        @Override
                                        public void onS(Response<Camera> response, Camera camera) {
                                            cameras.add(camera);
                                            isComplete++;
                                            if (isComplete == cId.length) {
                                                if (cId.length == 1) {
                                                    binding.tvDevice.setCompoundDrawables(null, null, null, null);
                                                } else {
                                                    Drawable drawable = getContext().getDrawable(R.drawable.ic_pull);
                                                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                                                    binding.tvDevice.setCompoundDrawables(null, null, drawable, null);
                                                }
                                                code = cameras.get(0).data.cameraCode;
                                                binding.tvDevice.setText(entity + cameras.get(0).data.cameraName + "摄像头");
                                            } else {
                                                binding.tvDevice.setCompoundDrawables(null, null, null, null);
                                            }
                                        }
                                    });
                        }

                    }

                });

    }

    private void cmd(View view) {
        if (isComplete == cId.length) {
            if (TextUtils.isEmpty(code)) {
                $.toast("请选择摄像头");
                return;
            }
            String direction = map.get(view.getId());
            BaseApp.api.deviceStart(access_token, code, direction)
                    .enqueue(new JCallback<Results>() {
                        @Override
                        public void onS(Response<Results> response, Results results) {
                            BaseApp.mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    BaseApp.api.deviceStop(access_token, code, direction)
                                            .enqueue(new JCallback<Results>() {
                                                @Override
                                                public void onS(Response<Results> response, Results results) {

                                                }
                                            });
                                }
                            }, 500);

                        }
                    });
        } else {
            $.toast("摄像头正在初始化");
        }

    }


}