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 android.view.Menu;
import android.view.MenuItem;

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 com.tofly.ghrq_route.common.util.Dialogs;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YCHK
 * 工地监护查询
 */
public class UiBuildCXActivity extends BaseUi {

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

    private BuildManageController bController;
    private BuildManageAdapter bAdapter;
    private int page = 1;
    private List<BuildManageBean> mBuildManageList;

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

    private void initData() {
        mBuildManageList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setItemAnimator(new DefaultItemAnimator());
        bAdapter = new BuildManageAdapter(this, mBuildManageList, 6);
        recycleView.setAdapter(bAdapter);
        getData(null);
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_message_outbox, menu);
        menu.findItem(R.id.action_new).setVisible(false);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
            Dialogs.show2(this, editText -> {
                if (!TextUtils.isEmpty(editText)) {
                    mBuildManageList.clear();
                    getData(editText);
                }
            });
        }
        return super.onOptionsItemSelected(item);
    }

    private void getData(String keyWord) {
        if (bController == null) {
            bController = new BuildManageController(this);
        }
        startRefreshing();
        recycleView.setRefreshing(false);
        bController.searchData(keyWord, 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);
                        bAdapter.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);
    }
}
