package xn.hxp.ui.plan.already;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
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 androidx.activity.result.ActivityResultLauncher;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.kongzue.dialogx.dialogs.PopTip;
import com.rc.httpcore.bean.HxpChemicalVo;

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

import xn.hxp.databinding.ItemAlreadyBinding;
import xn.hxp.ui.PrintBean;
import xn.hxp.ui.plan.change.ChangeCabinetActivity;
import xn.hxp.utils.PrintTool;
import xn.hxp.weidith.TextTool;

public class AlreadyAdapter extends BaseAdapter {
    private AlreadyActivity context;
    private List<HxpChemicalVo> recordsBeanList;
    private int type = 0; // 0:与自己相关 1:其它

    // 存储位置替换的回调
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;

    public AlreadyAdapter(AlreadyActivity context, List<HxpChemicalVo> recordsBeanList, ActivityResultLauncher<Intent> intentActivityResultLauncher) {
        this.context = context;
        this.recordsBeanList = recordsBeanList;
        this.intentActivityResultLauncher = intentActivityResultLauncher;
    }

    @Override
    public int getCount() {
        return recordsBeanList.size();
    }

    @Override
    public HxpChemicalVo getItem(int position) {
        return recordsBeanList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemAlreadyBinding binding = ItemAlreadyBinding.inflate(LayoutInflater.from(context), parent, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        try {
            HxpChemicalVo recordsBean = getItem(position);
            ItemAlreadyBinding binding = viewHolder.binding;
            // 申领单号
            String applyOrder = recordsBean.getApplyOrder();
            binding.alreadyCodeTV.setText(TextUtils.isEmpty(applyOrder) ? "" : applyOrder);
            // 化学品名称
            String chemicalName = recordsBean.getChemicalName();
            binding.name.setText(TextUtils.isEmpty(chemicalName) ? "" : chemicalName);
            binding.name.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    TextTool.showPop(context, binding.name);
                }
            });
            // 类别
            String chemicalCategoryName = recordsBean.getChemicalCategoryName();
            binding.type.setText(TextUtils.isEmpty(chemicalCategoryName) ? "" : chemicalCategoryName);
            // CAS
            String casNum = recordsBean.getCasNum();
            binding.cas.setText(TextUtils.isEmpty(casNum) ? "" : casNum);
            // 纯度
            String chemicalPurity = recordsBean.getChemicalPurity();
            binding.purity.setText(TextUtils.isEmpty(chemicalPurity) ? "" : chemicalPurity);
            // 规格
            String normsNum = recordsBean.getNormsNum();
            String normsUnit = recordsBean.getNormsUnit();
            binding.specs.setText(normsNum + (TextUtils.isEmpty(normsUnit) ? "" : normsUnit));
            // 申领人
            String applyUserName = recordsBean.getApplyUserName();
            binding.applicantTV.setText(TextUtils.isEmpty(applyUserName) ? "" : applyUserName);
            // 归属人
            boolean topicGroup = recordsBean.isTopicGroup();
            // 是否是课题组
            if (topicGroup) {
                String topicGroupName = recordsBean.getTopicGroupName();
                binding.belongTV.setText(TextUtils.isEmpty(topicGroupName) ? "" : topicGroupName);
            } else {
                binding.belongTV.setText(TextUtils.isEmpty(applyUserName) ? "" : applyUserName);
            }
            binding.belongTV.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    TextTool.showPop(context, binding.belongTV);
                }
            });
            // 编码
            String labels = recordsBean.getLabels();
            binding.code.setText(TextUtils.isEmpty(labels) ? "" : labels);
            // 存储位置
            String cabinetName = recordsBean.getCabinetName();
            String doorName = recordsBean.getDoorName();
            int doorLayers = recordsBean.getDoorLayers();
            binding.save.setText(cabinetName + "-" + doorName + "-" + doorLayers + "层");
            binding.selectCB.setChecked(recordsBean.isSelect());

            if (0 == type) {
                binding.save.setTextColor(Color.parseColor("#ff0183FA"));
                binding.operate.setTextColor(Color.parseColor("#ff0183FA"));
                binding.selectCB.setEnabled(true);
                binding.selectCB.setOnCheckedChangeListener(null);
                binding.selectCB.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        HxpChemicalVo recordsBean = getItem(position);
                        recordsBean.setSelect(binding.selectCB.isChecked());
                    }
                });
                binding.save.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        context.showLoading("");
                        recordsBeanList.get(position).setSelect(true);
                        notifyDataSetChanged();
                        intentActivityResultLauncher.launch(new Intent(context, ChangeCabinetActivity.class));
                        context.dismissLoading();
                    }
                });

                binding.operate.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {

                        context.showLoading("打印中...");
                        AsyncTask.execute(() -> {
                            try {
                                HxpChemicalVo printAlreadyBean = recordsBeanList.get(position);
                                printAlreadyBean.setSelect(true);
                                notifyDataSetChanged();
                                List<HxpChemicalVo> printBeanList = new ArrayList<>();
                                for (int i = 0; i < recordsBeanList.size(); i++) {
                                    HxpChemicalVo recordsBean1 = recordsBeanList.get(i);
                                    if (recordsBean1.isSelect()) {
                                        printBeanList.add(recordsBean1);
                                    }
                                }
                                for (int i = 0; i < printBeanList.size(); i++) {
                                    PrintBean printBean = new PrintBean();
                                    // 化学品编码
                                    HxpChemicalVo itemBean = printBeanList.get(i);
                                    String tagCode = itemBean.getLabels();
                                    printBean.setTag(TextUtils.isEmpty(tagCode) ? "" : tagCode);
                                    // 二维码
                                    printBean.setWxCode(TextUtils.isEmpty(tagCode) ? "" : tagCode);
                                    // 化学品名
                                    printBean.setName(TextUtils.isEmpty(itemBean.getChemicalName()) ? "" : itemBean.getChemicalName());
                                    // CAS
                                    String casNum1 = itemBean.getCasNum();
                                    printBean.setCasNo(TextUtils.isEmpty(casNum1) ? "" : casNum1);
                                    int belongType = itemBean.getBelongType();
                                    String name = "";
                                    // 是否是课题组
                                    if (itemBean.isTopicGroup()) {
                                        name = itemBean.getTopicGroupName();
                                    } else {
                                        name = itemBean.getApplyUserName();
                                    }
                                    printBean.setPerson(TextUtils.isEmpty(name) ? "" : name);
                                    // 化学品类型
                                    CharSequence chemicalCategory = itemBean.getChemicalCategoryName();
                                    printBean.setTypes(TextUtils.isEmpty(chemicalCategory) ? "" : chemicalCategory.toString());
                                    if (StringUtils.isEmpty(tagCode)) {
                                        PopTip.show("打印参数异常!");
                                    } else {
                                        PrintTool.INSTANCE.print(true, !itemBean.isTopicGroup(), printBean);
                                    }
                                }

                            } catch (Exception e) {
                                LogUtils.e(Log.getStackTraceString(e));
                            }
                            context.dismissLoading();
                            PopTip.show("打印完成!");
                        });
                    }
                });
            } else {
                binding.save.setTextColor(Color.parseColor("#ff333333"));
                binding.operate.setTextColor(Color.parseColor("#ff333333"));
                binding.selectCB.setEnabled(false);
                binding.save.setOnClickListener(null);
                binding.operate.setOnClickListener(null);
            }
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }

        return convertView;
    }

    public void update(int type) {
        this.type = type;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        ItemAlreadyBinding binding;

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