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.BuildManageAdapter;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageCallback;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageController;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YCHK
 * 工地监护归档
 */
public class UiBuildGdActivity extends BaseUi {

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

    private BuildManageController buildManageController;
    private BuildManageAdapter buildManageAdapter;
    private int page = 1;
    private List<BuildManageBean> 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());
        buildManageAdapter = new BuildManageAdapter(this, mBuildManageList, 5);
        recycleView.setAdapter(buildManageAdapter);
        getData();
    }

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

    private void getData() {
        if (buildManageController == null) {
            buildManageController = new BuildManageController(this);
        }
        startRefreshing();
        recycleView.setRefreshing(false);
        buildManageController.getDataList(5, page, 10, new BuildManageCallback() {
            @Override
            public void onGetBuildManageList(boolean isSuccess, int errorCode, String errMsg, List<BuildManageBean> buildManageList, int total) {
                super.onGetBuildManageList(isSuccess, errorCode, errMsg, buildManageList, total);
                stopRefreshing();
                recycleView.setRefreshing(true);
                if (isSuccess) {
                    if (buildManageList != null) {
                        mBuildManageList.addAll(buildManageList);
                        buildManageAdapter.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);
    }
}
