package com.sxyz.rfid.ui.review;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseFragment;
import com.sxyz.rfid.databinding.FragmentOutboundBinding;
import com.sxyz.rfid.databinding.FragmentReviewBinding;
import com.sxyz.rfid.presenter.IOutBoundPresenter;
import com.sxyz.rfid.presenter.IReviewPresenter;
import com.sxyz.rfid.presenter.imp.OutBoundPresenterImpl;
import com.sxyz.rfid.presenter.imp.ReviewPresenterImpl;
import com.sxyz.rfid.presenter.model.OutboundModel;
import com.sxyz.rfid.presenter.model.ReviewDetailListModel;
import com.sxyz.rfid.presenter.model.ReviewDetailModel;
import com.sxyz.rfid.presenter.model.ReviewModel;
import com.sxyz.rfid.presenter.model.ReviewResultModel;
import com.sxyz.rfid.presenter.model.ReviewRfidTaskCheckModel;
import com.sxyz.rfid.presenter.view.IReviewView;
import com.sxyz.rfid.ui.out.OutboundActivity;
import com.sxyz.rfid.ui.out.OutboundDetailActivity;
import com.sxyz.rfid.ui.out.adapter.OutBoundAdapter;
import com.sxyz.rfid.ui.review.adapter.ReviewAdapter;
import com.sxyz.rfid.utils.StringUtil;
import com.sxyz.rfid.utils.ToastUtil;
import com.sxyz.rfid.widgets.AlertDialogUtil;
import com.sxyz.rfid.widgets.CustomLoadMoreView;

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

/**
 * 主要功能: 盘点
 *
 * @Description:
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.review.ReviewFragment
 * @author: chunyuanwu
 * @date: 2025年12月29日 13:00
 * @version: 1.0.0
 */
public class ReviewFragment extends BaseFragment<FragmentReviewBinding> implements IReviewView, OnLoadMoreListener {
    private ReviewAdapter mAdapter;
    private List<ReviewModel.RowsBean> list = new ArrayList<>();
    private int page = 1;
    private IReviewPresenter mPresenter;

    public static Fragment newInstance() {
        return new ReviewFragment();
    }

    @Override
    public void initParms(Bundle mBundle) {

    }

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

    @Override
    protected void initView() {
        initToolBarTitle("盘点列表");
        mPresenter = new ReviewPresenterImpl(this);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new ReviewAdapter(R.layout.item_review, list);
        mAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        mBinding.recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void setListener() {
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(this);
        mAdapter.getLoadMoreModule().setAutoLoadMore(true);
//        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        mAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(true);
        mBinding.swipeRefreshLayout.setOnRefreshListener(this::initData);
        mAdapter.setOnItemClickListener(new ReviewAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(int position, ReviewModel.RowsBean item) {
                //弹窗提示
                new AlertDialog.Builder(mActivity).setTitle("提示").setMessage("确定删除该盘点任务吗？").setPositiveButton("确定", (dialog, which) -> {
                    mPresenter.doDeleteReviewTask(ReviewFragment.this, item.getTaskId());
                }).setNegativeButton("取消", null).show();
            }

            @Override
            public void onItemClick(int position, ReviewModel.RowsBean item) {
                // 处理详情点击事件
                Bundle bundle = new Bundle();
                bundle.putSerializable("item", item);
                startActivity(ReviewDetailActivity.class, bundle);
            }

            @Override
            public void onCopyClick(int position, ReviewModel.RowsBean item) {
                StringUtil.copyToClipboard(mActivity, item.getTaskNo());
            }

            @Override
            public void onReviewTaskClick(int position, ReviewModel.RowsBean item) {
                // 处理出库点击事件
                Bundle bundle = new Bundle();
                bundle.putSerializable("item", item);
                startActivity(ReviewTaskActivity.class, bundle);
            }

            // 处理结束盘点点击事件
            @Override
            public void onStopTaskClick(int position, ReviewModel.RowsBean item) {
                new AlertDialog.Builder(mActivity).setTitle("提示").setMessage("确定结束该盘点任务吗？").setPositiveButton("确定", (dialog, which) -> {
                    mPresenter.doStopReviewTask(ReviewFragment.this, item.getTaskId());
                }).setNegativeButton("取消", null).show();
            }
        });
        mBinding.searchEtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    mBinding.ivSearch.setVisibility(VISIBLE);
                } else {
                    mBinding.ivSearch.setVisibility(GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.ivSearch.setOnClickListener(v -> {
            mBinding.searchEtInput.setText("");
            initData();
        });
        mBinding.searchEtInput.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                initData();
                return true;
            }
            return false;
        });
    }

    @Override
    protected void initData() {
        page = 1;
        mPresenter.doReviewList(this, 10, page, mBinding.searchEtInput.getText().toString());
    }

    @Override
    public void reviewSuccess(ReviewModel model) {
        list.clear();
        mAdapter.setList(model.getRows());
        mAdapter.getLoadMoreModule().setEnableLoadMore(true);
        mBinding.swipeRefreshLayout.setRefreshing(false);
        int totalPages = StringUtil.totalPage(model.getTotal(), 10);
        if (totalPages == page) {
            mAdapter.getLoadMoreModule().loadMoreEnd();
        } else {
            mAdapter.getLoadMoreModule().loadMoreComplete();
        }
        if (model.getRows().isEmpty()) {
            mBinding.includedNothing.llNothing.setVisibility(View.VISIBLE);
        } else {
            mBinding.includedNothing.llNothing.setVisibility(View.GONE);
        }
    }

    @Override
    public void reviewLoadMoreSuccess(ReviewModel model) {
        mAdapter.addData(model.getRows());
        int totalPages = StringUtil.totalPage(model.getTotal(), 10);
        if (totalPages == page) {
            mAdapter.getLoadMoreModule().loadMoreEnd();
        } else {
            mAdapter.getLoadMoreModule().loadMoreComplete();
        }
    }

    @Override
    public void reviewDetailSuccess(ReviewDetailModel model) {

    }

    @Override
    public void reviewDetailListSuccess(ReviewDetailListModel model) {

    }

    @Override
    public void reviewResultSuccess(ReviewResultModel model) {

    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.doReviewList(this, 10, page, mBinding.searchEtInput.getText().toString());
    }

    @Override
    public void deleteReviewTaskSuccess() {
        ToastUtil.showShort("删除成功");
        mPresenter.doReviewList(this, 10, page, mBinding.searchEtInput.getText().toString());
    }

    @Override
    public void reviewScanTaskCheckSuccess(ReviewRfidTaskCheckModel model) {

    }

    @Override
    public void reviewRfidTaskCheckSuccess(ReviewRfidTaskCheckModel model) {

    }

    /**
     * 结束盘点成功
     */
    @Override
    public void stopReviewTaskSuccess() {
        ToastUtil.showShort("结束盘点成功");
        mPresenter.doReviewList(this, 10, page, mBinding.searchEtInput.getText().toString());
    }

}
