package com.sxyz.rfid.ui.out;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

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

import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseFragment;
import com.sxyz.rfid.databinding.FragmentOutboundBinding;
import com.sxyz.rfid.greendao.GreenDaoManager;
import com.sxyz.rfid.greendao.model.Out;
import com.sxyz.rfid.presenter.IDictPresenter;
import com.sxyz.rfid.presenter.IOutBoundPresenter;
import com.sxyz.rfid.presenter.imp.DictPresenterImpl;
import com.sxyz.rfid.presenter.imp.OutBoundPresenterImpl;
import com.sxyz.rfid.presenter.model.DictModel;
import com.sxyz.rfid.presenter.model.OutBoundDetailModel;
import com.sxyz.rfid.presenter.model.OutboundFlowingWaterModel;
import com.sxyz.rfid.presenter.model.OutboundModel;
import com.sxyz.rfid.presenter.view.IDictView;
import com.sxyz.rfid.presenter.view.IOutBoundView;
import com.sxyz.rfid.ui.MainActivity;
import com.sxyz.rfid.ui.out.adapter.OutBoundAdapter;
import com.sxyz.rfid.utils.L;
import com.sxyz.rfid.utils.StringUtil;
import com.sxyz.rfid.widgets.CustomLoadMoreView;

import org.greenrobot.greendao.query.QueryBuilder;

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

import sql.com.yz.db.DaoSession;

/**
 * 主要功能:
 *
 * @Description:
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui
 * @author: chunyuanwu
 * @date: 2025年12月29日 13:00
 * @version: 1.0.0
 */
public class OutboundFragment extends BaseFragment<FragmentOutboundBinding> implements IOutBoundView, OnLoadMoreListener {

    private OutBoundAdapter mOutBoundAdapter;
    private List<OutboundModel.RowsBean> list = new ArrayList<>();
    private IOutBoundPresenter mPresenter;
    private int page = 1;

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

    @Override
    public void initParms(Bundle mBundle) {

    }

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

    @Override
    protected void initView() {
        initToolBarTitle("出库管理");
        mPresenter = new OutBoundPresenterImpl(this);
        mPresenter.doOutBoundList(this, 10, 1);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mOutBoundAdapter = new OutBoundAdapter(R.layout.item_outbound, list);
        mOutBoundAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        mBinding.recyclerView.setAdapter(mOutBoundAdapter);
//        XXPermissions.with(getActivity())
//                .permission(Permission.WRITE_EXTERNAL_STORAGE)
//                .permission(Permission.READ_EXTERNAL_STORAGE)
//                .permission(Permission.READ_CONTACTS)
//                .request(new OnPermission() {
//                    @Override
//                    public void hasPermission(List<String> granted, boolean isAll) {
//                    }
//
//                    @Override
//                    public void noPermission(List<String> denied, boolean quick) {
//                    }
//                });

        Out model = new Out();
        model.setName("111");
        DaoSession daoSession = GreenDaoManager.getInstance().getDaoSession();
        daoSession.insert(model);

//        List<Out> listPage = daoSession.queryBuilder(Out.class).list();
//        L.i("listPage = " + listPage);

    }

    @Override
    protected void setListener() {
        mOutBoundAdapter.getLoadMoreModule().setOnLoadMoreListener(this);
        mOutBoundAdapter.getLoadMoreModule().setAutoLoadMore(true);
//        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        mOutBoundAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(true);
        mBinding.swipeRefreshLayout.setOnRefreshListener(this::initData);
        mOutBoundAdapter.setOnItemClickListener(new OutBoundAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, OutboundModel.RowsBean item) {
                // 处理详情点击事件
                Bundle bundle = new Bundle();
                bundle.putSerializable("item", item);
                startActivity(OutboundDetailActivity.class, bundle);
            }

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

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

    @Override
    protected void initData() {
        page = 1;
        mPresenter.doOutBoundList(this, 10, page);
    }

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

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

    @Override
    public void outBoundDetailSuccess(OutBoundDetailModel model) {

    }

    @Override
    public void outBoundFlowingWaterSuccess(OutboundFlowingWaterModel model) {

    }

    @Override
    public void outBoundCancelSuccess() {

    }

    @Override
    public void outBoundDetailCancelSuccess() {

    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.doOutBoundList(this, 10, page);
    }
}
