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

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

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.common.ui.adapter.RepairHistoryAdapter;
import com.tofly.ghrq_route.module.planPointRepair.controller.PointRepairCallBack;
import com.tofly.ghrq_route.module.planPointRepair.controller.PointRepairController;
import com.tofly.ghrq_route.module.planPointRepair.domain.PlanPointRepair;
import com.tofly.ghrq_route.module.user.domain.UserBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/6/7.
 */

public class UiRepairHistory extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;

    private UserBean mUser;

    private PointRepairController repairController;

    private List<PlanPointRepair> mRepairHistoryList;

    private RepairHistoryAdapter repairHistoryAdapter;

    private boolean mbIsRefreshing = false;

    private int pageindex = 1;

    private final int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_repair_history);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();

        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void onMessageEvent(UiRepairMapStep.PointRepairEvent event){
        if (event.message.equals(UiRepairMapStep.POINT_REPAIR_REMOVED) || event.message.equals(UiRepairMapStep.POINT_REPAIR_UPDATED)){
            recyclerView.setRefreshing(true);
            loadRepairHistoryList();
        }
    }

    private void initView() {

    }

    private void initData() {
        mUser = BaseApp.getInstance().getLoginUser();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        mRepairHistoryList = new ArrayList<>();
        repairHistoryAdapter = new RepairHistoryAdapter(this, mRepairHistoryList);
        recyclerView.setAdapter(repairHistoryAdapter);

        recyclerView.setRefreshing(true);
        loadRepairHistoryList();
    }

    private void initListener() {
        recyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!mbIsRefreshing){
                    loadRepairHistoryList();
                }
            }
        });
        repairHistoryAdapter.setOnItemClickListener(new ARecyclerBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                PlanPointRepair pointRepair = repairHistoryAdapter.getItem(position);
                if (pointRepair != null){
                    Intent intent = new Intent();
                    intent.setClass(UiRepairHistory.this, UiRepairUpdateStep.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("point_repair", pointRepair);
                    bundle.putBoolean("is_update", true);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
    }

    private void loadRepairHistoryList(){
        if (repairController == null)
            repairController = new PointRepairController(this);
        mbIsRefreshing = true;
        repairController.getPointRepairList(mUser.getToken(), pageindex, pagesize, new PointRepairCallBack(){
            @Override
            public void onGetPointRepairList(boolean isSuccess, int errorCode, String errMsg, List<PlanPointRepair> repairList, int total) {
                super.onGetPointRepairList(isSuccess, errorCode, errMsg, repairList, total);
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setRefreshing(false);
                        recyclerView.hideMoreProgress();
                        mbIsRefreshing = false;
                    }
                }, 2000);
                if (isSuccess && repairList != null) {
                    if (repairList.size() > 0) {
                        recyclerView.setTotal(total);
                        repairHistoryAdapter.setData(repairList);
                    } else {
                        recyclerView.setTotal(0);
                        repairHistoryAdapter.clear();
                    }
                } else {
                    recyclerView.setTotal(0);
                    repairHistoryAdapter.clear();
                    toast(errMsg);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
