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.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 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 UiQueryActivity 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_query_repair);
        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, 6);
        recycleView.setAdapter(repairAdapter);
        getData(null);
    }

    private void initList() {
        repairAdapter.setOnItemClickListener((view, position) -> {
            RushRepairBean buildManageBean = repairAdapter.getItem(position);
            if (!TextUtils.isEmpty(buildManageBean.getId())) {
                Intent intent = new Intent(UiQueryActivity.this, UiRepairDetail.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.show1(this, editText -> {
                if (!TextUtils.isEmpty(editText)) {
                    mBuildManageList.clear();
                    getData(editText);
                }
            });
        }
        return super.onOptionsItemSelected(item);
    }

    private void getData(String keyWord) {
        if (rController == null) {
            rController = new RushRepairController(this);
        }
        startRefreshing();
        recycleView.setRefreshing(false);
        rController.searchData(keyWord, 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) {
                        recycleView.setTotal(total);
                        mBuildManageList.addAll(buildManageList);
                        repairAdapter.setData(mBuildManageList);
                    }
                }
            }
        });
    }

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

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