package com.example.chemical.ui.plan.unlock;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.blankj.utilcode.util.LogUtils;
import com.example.chemical.R;
import com.example.chemical.databinding.ItemDoorUnlockBinding;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetDoorVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetLockVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetVo;
import com.kongzue.dialogx.dialogs.PopTip;

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

public class UnlockAdapter extends BaseAdapter {
    List<HxpCabinetVo> cabinetList;
    List<HxpCabinetDoorVo> doorList;
    private UnlockActivity activity;
    private boolean isUnlockEnd = false;

    public UnlockAdapter(UnlockActivity activity, List<HxpCabinetVo> cabinetList, List<HxpCabinetDoorVo> doorList) {
        this.cabinetList = cabinetList;
        this.doorList = doorList;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return null == cabinetList ? 0 : cabinetList.size();
    }

    @Override
    public HxpCabinetVo getItem(int position) {
        return null == cabinetList ? null : cabinetList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HxpCabinetVo cabinet = getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemDoorUnlockBinding binding = ItemDoorUnlockBinding.inflate(LayoutInflater.from(activity), parent, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
            // 上门
            binding.upDoorUnlockBT.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != cabinet) {
                        List<HxpCabinetDoorVo> hxpCabinetDoorVoList = findDoorByCabinetId(cabinet.getCabinetId());
                        if (!hxpCabinetDoorVoList.isEmpty()) {
                            HxpCabinetDoorVo upDoor = hxpCabinetDoorVoList.get(0);
                            List<HxpCabinetLockVo> lockVoList = upDoor.getCabinetLockVoList();
                            if (null != lockVoList && !lockVoList.isEmpty()) {
                                activity.activityHelp.unlock(lockVoList.get(0));
                                return;
                            }
                        }
                        PopTip.show("开门失败!");
                    }
                }
            });
            // 下门
            binding.downDoorUnlockBT.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != cabinet) {
                        List<HxpCabinetDoorVo> hxpCabinetDoorVoList = findDoorByCabinetId(cabinet.getCabinetId());
                        if (!hxpCabinetDoorVoList.isEmpty()) {
                            HxpCabinetDoorVo downDoor = hxpCabinetDoorVoList.get(1);
                            List<HxpCabinetLockVo> lockVoList = downDoor.getCabinetLockVoList();
                            if (null != lockVoList && !lockVoList.isEmpty()) {
                                activity.activityHelp.unlock(lockVoList.get(0));
                                return;
                            }
                        }
                        PopTip.show("开门失败!");
                    }
                }
            });
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (null != cabinet) {
            try {
                String cabinetName = cabinet.getCabinetName();
                viewHolder.binding.cabinetNameTV.setText(TextUtils.isEmpty(cabinetName) ? "" : cabinetName);

                List<HxpCabinetDoorVo> hxpCabinetDoorVoList = findDoorByCabinetId(cabinet.getCabinetId());
                int doorNum = cabinet.getDoorNum();
                HxpCabinetDoorVo upDoor = hxpCabinetDoorVoList.get(0);
                String doorName = upDoor.getDoorName();
                viewHolder.binding.upDoorNameTV.setText(TextUtils.isEmpty(doorName) ? "" : doorName);

                if (doorNum > 1) {
                    viewHolder.binding.cabinetLL.setBackgroundResource(R.mipmap.img_skm);
                } else {
                    viewHolder.binding.cabinetLL.setBackgroundResource(R.mipmap.img_dkm);
                }

                // 上柜门开锁
                List<HxpCabinetLockVo> upLockList = upDoor.getCabinetLockVoList();
                HxpCabinetLockVo upLock = upLockList.get(0);
                switch (upLock.getUnlockType()) {
                    // 0: 未开过锁
                    case 0:
                        viewHolder.binding.upDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_zh);
                        viewHolder.binding.upDoorNameTV.setTextColor(Color.parseColor("#FF333333"));
                        viewHolder.binding.upDoorUnlockBT.setVisibility(View.GONE);
                        break;
                    // 1: 开锁并开锁成功
                    case 1:
                        viewHolder.binding.upDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_xz);
                        viewHolder.binding.upDoorNameTV.setTextColor(Color.parseColor("#FF0FB525"));
                        viewHolder.binding.upDoorUnlockBT.setVisibility(View.GONE);
                        break;
                    // 2: 开过锁但开锁失败
                    case 2:
                        viewHolder.binding.upDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_cw);
                        viewHolder.binding.upDoorNameTV.setTextColor(Color.parseColor("#FFFF2E2E"));
                        viewHolder.binding.upDoorUnlockBT.setVisibility(View.GONE);
                        break;
                    // 3: 开锁中
                    case 3:
                        viewHolder.binding.upDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_zh);
                        viewHolder.binding.upDoorNameTV.setTextColor(Color.parseColor("#FF333333"));
                        viewHolder.binding.upDoorUnlockBT.setVisibility(View.GONE);
                        break;
                }
                if (isUnlockEnd) {
                    viewHolder.binding.upDoorUnlockBT.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.binding.upDoorUnlockBT.setVisibility(View.GONE);
                }

                if (hxpCabinetDoorVoList.size() > 1) {
                    HxpCabinetDoorVo downDoor = hxpCabinetDoorVoList.get(1);
                    String downDoorName = downDoor.getDoorName();
                    viewHolder.binding.downDoorNameTV.setText(TextUtils.isEmpty(downDoorName) ? "" : downDoorName);
                    viewHolder.binding.downDoorUnlockLL.setVisibility(View.VISIBLE);
                    // 下柜们开锁
                    List<HxpCabinetLockVo> downLockList = downDoor.getCabinetLockVoList();
                    HxpCabinetLockVo downLock = downLockList.get(0);
                    switch (downLock.getUnlockType()) {
                        // 0: 未开过锁
                        case 0:
                            viewHolder.binding.downDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_zh);
                            viewHolder.binding.downDoorNameTV.setTextColor(Color.parseColor("#FF333333"));
                            viewHolder.binding.downDoorUnlockBT.setVisibility(View.GONE);
                            break;
                        // 1: 开锁并开锁成功
                        case 1:
                            viewHolder.binding.downDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_xz);
                            viewHolder.binding.downDoorNameTV.setTextColor(Color.parseColor("#FF0FB525"));
                            viewHolder.binding.downDoorUnlockBT.setVisibility(View.GONE);
                            break;
                        // 2: 开过锁但开锁失败
                        case 2:
                            viewHolder.binding.downDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_cw);
                            viewHolder.binding.downDoorNameTV.setTextColor(Color.parseColor("#FFFF2E2E"));
                            viewHolder.binding.downDoorUnlockBT.setVisibility(View.GONE);
                            break;
                        // 3: 开锁中
                        case 3:
                            viewHolder.binding.downDoorNameIV.setBackgroundResource(R.mipmap.icon_gzxx_zh);
                            viewHolder.binding.downDoorNameTV.setTextColor(Color.parseColor("#FF333333"));
                            viewHolder.binding.downDoorUnlockBT.setVisibility(View.GONE);
                            break;
                    }

                    if (isUnlockEnd) {
                        viewHolder.binding.downDoorUnlockBT.setVisibility(View.VISIBLE);
                    } else {
                        viewHolder.binding.downDoorUnlockBT.setVisibility(View.GONE);
                    }
                }

            } catch (Exception e) {
                LogUtils.e(Log.getStackTraceString(e));
            }
        }

        return convertView;
    }

    private List<HxpCabinetDoorVo> findDoorByCabinetId(long cabinetId) {
        List<HxpCabinetDoorVo> doorVoList = new ArrayList<>();
        for (int i = 0; i < doorList.size(); i++) {
            HxpCabinetDoorVo doorVo = doorList.get(i);
            if (cabinetId == doorVo.getCabinetId()) {
                doorVoList.add(doorVo);
            }
        }
        return doorVoList;
    }


    public void setUnlockEnd(boolean isUnlockEnd) {
        this.isUnlockEnd = isUnlockEnd;
    }

    static class ViewHolder {
        ItemDoorUnlockBinding binding;

        public ViewHolder(ItemDoorUnlockBinding binding) {
            this.binding = binding;
        }
    }
}
