package com.tofly.ghrq_route.common.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.adapter.RepairAdapter;
import com.tofly.ghrq_route.module.rushrepair.controller.RushRepairCallback;
import com.tofly.ghrq_route.module.rushrepair.controller.RushRepairController;
import com.tofly.ghrq_route.module.rushrepair.domain.RushRepairBean;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YCHK
 *  抢维修归档
 */
public class UiGuiDangActivity extends BaseUi {

    @BindView(R.id.recycleView)
    ARecycleView recycleView;

    private RushRepairController rController;
    private RepairAdapter repairAdapter;
    private int page=1;
    private List<RushRepairBean> mBuildManageList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gui_dang);
        ButterKnife.bind(this);
        initData();
        initList();
    }

    private void initData() {
        mBuildManageList= new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setItemAnimator(new DefaultItemAnimator());
        repairAdapter=new RepairAdapter(this,mBuildManageList,5);
        recycleView.setAdapter(repairAdapter);
        getData();
    }

    private void initList() {
        repairAdapter.setOnItemClickListener((view, position) -> {
            RushRepairBean buildManageBean = repairAdapter.getItem(position);
            if (!TextUtils.isEmpty(buildManageBean.getId())){
                Intent intent=new Intent(UiGuiDangActivity.this,UiRepairFile.class);
                intent.putExtra("id",buildManageBean.getId());
                startActivity(intent);
            }
        });
        recycleView.setOnRefreshListener(() -> {
            page=1;
            mBuildManageList.clear();
            getData();
        });
        recycleView.setOnLoadMoreListener(() -> {
            page++;
            getData();
        });
    }

    private void getData(){
        if (rController==null){
            rController=new RushRepairController(this);
        }
        startRefreshing();
        recycleView.setRefreshing(false);
        rController.getDataList(5,page,10,new RushRepairCallback(){
            @Override
            public void onRepairList(boolean isSuccess, int errorCode, String errMsg, List<RushRepairBean> buildManageList, int total) {
                super.onRepairList(isSuccess, errorCode, errMsg, buildManageList, total);
                stopRefreshing();
                recycleView.setRefreshing(true);
                if (isSuccess){
                    if (buildManageList!=null){
                        mBuildManageList.addAll(buildManageList);
                        repairAdapter.setData(mBuildManageList);
                        recycleView.setTotal(total);
                    }
                }
            }
        });
    }

    private void startRefreshing() {
        recycleView.setRefreshing(true);
    }

    private void stopRefreshing() {
        Handler handler = new Handler();
        handler.postDelayed(() -> {
            recycleView.setRefreshing(false);
            recycleView.hideMoreProgress();
        }, 500);
    }
}
