package com.sxyz.rfid.ui.review.offline;

import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityReviewDetailBinding;
import com.sxyz.rfid.greendao.GreenDaoManager;
import com.sxyz.rfid.greendao.model.WmsInventoryTask;
import com.sxyz.rfid.greendao.model.WmsInventoryTaskDetail;
import com.sxyz.rfid.ui.review.offline.adapter.ReviewOfflineDetailAdapter;
import com.sxyz.rfid.utils.L;
import com.sxyz.rfid.utils.StringUtil;

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

import sql.com.yz.db.DaoSession;
import sql.com.yz.db.WmsInventoryTaskDetailDao;

/**
 * 主要功能:  盘点详情 、 任务基础信息、任务明细信息
 *
 * @Description: 盘点详情 、 任务基础信息、任务明细信息
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.review
 * @author: chunyuanwu
 * @date: 2025年12月29日 18:18
 * @version: 1.0.0
 */
public class ReviewOfflineDetailActivity extends BaseActivity<ActivityReviewDetailBinding> {
    private WmsInventoryTask item;
    private ReviewOfflineDetailAdapter mAdapter;
    private List<WmsInventoryTaskDetail> mList = new ArrayList<>();
    private DaoSession daoSession;

    @Override
    public void initParms(Bundle mBundle) {
        item = (WmsInventoryTask) mBundle.getSerializable("item");
    }

    @Override
    protected ActivityReviewDetailBinding getViewBinding() {
        return ActivityReviewDetailBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        initToolBar("盘点详情");
        mBinding.rvOutboundDetails.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new ReviewOfflineDetailAdapter(R.layout.item_review_detail, mList);
        mBinding.rvOutboundDetails.setAdapter(mAdapter);
    }

    @Override
    protected void setListener() {
        //复制按钮点击事件
        mBinding.tvTaskNo.setOnClickListener(v -> {
            StringUtil.copyToClipboard(this, item.getTaskNo());
        });

        //出库明细点击事件
        mAdapter.setOnItemClickListener(new ReviewOfflineDetailAdapter.OnItemClickListener() {
            @Override
            public void onCopy1ItemClick(int position, WmsInventoryTaskDetail item) {
                StringUtil.copyToClipboard(mActivity, item.getBatchNo());
            }

            @Override
            public void onCopy2ItemClick(int position, WmsInventoryTaskDetail item) {
                StringUtil.copyToClipboard(mActivity, item.getRfidTag());
            }
        });
    }

    @Override
    protected void initData() {
        //任务编号
        mBinding.tvTaskNo.setText(item.getTaskNo());
        switch (item.getTaskStatus()) {
            case "0":
                mBinding.tvTaskStatus.setText("未完成");
                mBinding.tvTaskStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_red_bg));
                mBinding.tvTaskStatus.setTextColor(ContextCompat.getColor(this, R.color.red));
                break;
            case "1":
                mBinding.tvTaskStatus.setText("进行中");
                mBinding.tvTaskStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_org_bg));
                mBinding.tvTaskStatus.setTextColor(ContextCompat.getColor(this, R.color.text_orange3));
                break;
            case "2":
                mBinding.tvTaskStatus.setText("已完成");
                mBinding.tvTaskStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_green_bg));
                mBinding.tvTaskStatus.setTextColor(ContextCompat.getColor(this, R.color.green));
                break;
            case "3":
                mBinding.tvTaskStatus.setText("已结束");
                mBinding.tvTaskStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_gray_bg));
                mBinding.tvTaskStatus.setTextColor(ContextCompat.getColor(this, R.color.c_6666));
                break;
            default:
                mBinding.tvTaskStatus.setText("-");
                mBinding.tvTaskStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_gray_bg));
                mBinding.tvTaskStatus.setTextColor(ContextCompat.getColor(this, R.color.c_6666));
                break;
        }
        //任务名称
        mBinding.tvTaskName.setText(!StringUtils.isEmpty(item.getTaskName()) ? item.getTaskName() : "-");
        //仓库名称
        mBinding.txtWarehouseName.setText(!StringUtils.isEmpty(item.getWarehouseName()) ? item.getWarehouseName() : "-");
        //已盘数
        mBinding.txtReviewNumber.setText(item.getCountedItems() + "");
        //总盘点数
        mBinding.txtTotalNumber.setText(item.getTotalItems() + "");
        //出库人
        mBinding.txtRemark.setText(!StringUtils.isEmpty(item.getRemark()) ? item.getRemark() : "-");

        daoSession = GreenDaoManager.getInstance().getDaoSession();
        if (daoSession == null) {
            //数据库不存在
            mList.clear();
            mAdapter.notifyDataSetChanged();
            mBinding.includedNoDb.llNoDb.setVisibility(View.VISIBLE);
        } else {
            //根据item.getTaskId()查询任务明细
            mList = daoSession.getWmsInventoryTaskDetailDao().queryBuilder()
                    .where(WmsInventoryTaskDetailDao.Properties.TaskId.eq(item.getTaskId()))
                    .list();
            //设置新的数据集
            mAdapter.setNewInstance(mList);
            //隐藏暂无数据布局
            mBinding.includedNoDb.llNoDb.setVisibility(View.GONE);
            //判断是否为空 如果为空 则显示暂无数据 否则 隐藏暂无数据
            if (mList.isEmpty()) {
                mBinding.includedNothing.llNothing.setVisibility(View.VISIBLE);
            } else {
                mBinding.includedNothing.llNothing.setVisibility(View.GONE);
            }
        }
        L.i("list ======\n " + new Gson().toJson(mList));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (daoSession != null) {
            try {
                daoSession.clear();
                GreenDaoManager.getInstance().closeConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
