package com.sxyz.rfid.ui.out;

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

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;

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.OutboundFridModel;
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);
        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());
            }
        });
        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("");
            mPresenter.doOutBoundList(this, 10, 1, "");
        });
        mBinding.searchEtInput.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mPresenter.doOutBoundList(this, 10, 1, mBinding.searchEtInput.getText().toString());
                return true;
            }
            return false;
        });

    }

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

    @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.includedNothing.llNothing.setVisibility(View.VISIBLE);
        } else {
            mBinding.includedNothing.llNothing.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 outBoundFridSuccess(OutboundFridModel model) {

    }

    @Override
    public void outSubmitBoundFridSuccess() {

    }

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