package com.wd.workoffice.ui.fg;

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.ContactsBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.contract.WorkContactsContract;
import com.wd.workoffice.presenter.WorkContactsPresenter;
import com.wd.workoffice.ui.activity.bat.work.WorkContactsDetailActivity;
import com.wd.workoffice.ui.adapter.WorkContactsAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkContactsFragment extends WorkBaseFg implements WorkContactsContract.View {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private String partTypeNames;
    private int page = 1;
    private Map<String, Object> param;
    private WorkContactsPresenter workProPresenter;
    private WorkContactsAdapter workProductAdapter;
    private List<ContactsBean> dataList;

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        workProPresenter = new WorkContactsPresenter();
        workProPresenter.onAttach(this);
        rvData.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        param = WorkUtils.pageKey();
        param.put("partTypeNames", partTypeNames);
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        workProductAdapter = new WorkContactsAdapter(R.layout.item_work_contacts, dataList);
        workProductAdapter.bindToRecyclerView(rvData);
        workProductAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        workProPresenter.getData(param);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            partTypeNames = arguments.getString("partTypeNames");
        }
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("current", page);
            workProPresenter.getData(param);
        });
        workProductAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            workProPresenter.getData(param);
        }, rvData);
        workProductAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!WorkUtils.hasPermission(PagePermissionType.TRANSIT_PART_DETAILS.getPermission())) {
                    toast(R.string.permission);
                    return;
                }
                startActivity(WorkContactsDetailActivity.class,
                        "id",dataList.get(position).getId()+"",
                        "partTypeNames", partTypeNames,"title",dataList.get(position).getName());
            }
        });
    }


    @Override
    protected int layoutId() {
        return R.layout.fg_work_contacts;
    }

    @Override
    public void getData(List<ContactsBean> data) {
        if (page == 1) {
            dataList.clear();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
            workProductAdapter.loadMoreComplete();
            srlRefresh.finishRefresh();
        } else {
            workProductAdapter.loadMoreComplete();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
        }
        if (data.size() == 0) {
            workProductAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }
}
