package com.tofly.ghrq_route.module.trouble.ui;

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

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

import com.lcp.arecyclerview.widget.ARecycleView;
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.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.trouble.TroubleTool;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePlanRecordBean;
import com.tofly.ghrq_route.common.ui.activity.UiReportedClear;
import com.tofly.ghrq_route.common.ui.adapter.PlanTroubleRecordAdapter;
import com.tofly.ghrq_route.common.util.EventBusUtil;

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 UiTroubleRecordHistory extends BaseUi {
    @BindView(R.id.layout_select_area)
    LinearLayout layoutSelectArea;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;

    private TroubleBean mTrouble;

    private OfflinePathBean mPlanPoint;

    private PlanTroubleRecordAdapter troubleRecordAdapter;

    private TroubleTool troubleTool;

    private boolean mbIsClear = false;

    private boolean mbIsEditable = false;

    private int pageindex = 1;

    @Subscribe
    public void onMessageEvent(UiReportedClear.ReportClearMessageEvent messageEvent){
        if ( messageEvent.message.equals(UiReportedClear.REPORT_ROUTE_UPLOADED)||
                messageEvent.message.equals(UiReportedClear.REPORT_ROUTE_SAVED) ){
            pageindex = 1;
            loadPlanTroubleRecord();
        }else if (messageEvent.message.equals(UiReportedClear.REPORT_CLEAR_UPLOADED)||
                messageEvent.message.equals(UiReportedClear.REPORT_CLEAR_SAVED)){
            if (mbIsClear){
                pageindex = 1;
                loadPlanTroubleClearRecord();
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_plan);
        ButterKnife.bind(this);

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

        EventBusUtil.register(this);
    }

    private void initView() {
        layoutSelectArea.setVisibility(View.GONE);
    }


    private void initData() {
        troubleTool = TroubleTool.getInstance();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mTrouble = (TroubleBean) bundle.getSerializable(TroubleBean.TROUBLE_BEAN_KEY);
            mPlanPoint = (OfflinePathBean) bundle.getSerializable(OfflinePathBean.PLAN_POINT_BEAN_KEY);
            mbIsClear = bundle.getBoolean(TroublePlanRecordBean.TROUBLE_RECORD_CLOSE_KEY, false);
            mbIsEditable = bundle.getBoolean(TroublePlanRecordBean.TROUBLE_RECORD_EDITABLE_KEY, false);
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        List<TroublePlanRecordBean> list = new ArrayList<>();
        troubleRecordAdapter = new PlanTroubleRecordAdapter(this, list, mbIsClear);
        recyclerView.setAdapter(troubleRecordAdapter);
        recyclerView.setRefreshing(true);

        if (mbIsClear){
            loadPlanTroubleClearRecord();
        }else {
            loadPlanTroubleRecord();
        }
    }

    private void initListener() {
        recyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageindex = 1;
                if (mbIsClear){
                    loadPlanTroubleClearRecord();
                }else {
                    loadPlanTroubleRecord();
                }
            }
        });

        recyclerView.setOnLoadMoreListener(new ARecycleView.OnLoadMoreListener() {
            @Override
            public void loadMore() {
                pageindex++;
                if (mbIsClear){
                    loadPlanTroubleClearRecord();
                }else {
                    loadPlanTroubleRecord();
                }
            }
        });
        troubleRecordAdapter.setOnItemClickListener(new PlanTroubleRecordAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                TroublePlanRecordBean recordBean = troubleRecordAdapter.getItem(position);
                gotoUiRecordDetail(recordBean);
            }
        });
    }

    private void stopRefreshing(){
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.setRefreshing(false);
                recyclerView.hideMoreProgress();
            }
        }, 2000);
    }

    public void loadPlanTroubleRecord(){

    }

    public void loadPlanTroubleClearRecord(){

    }

    public void gotoUiRecordDetail(TroublePlanRecordBean recordBean) {
        if (recordBean != null){
            Bundle bundle = new Bundle();
            bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, mTrouble);
            bundle.putSerializable(OfflinePathBean.PLAN_POINT_BEAN_KEY, mPlanPoint);
            bundle.putSerializable(TroublePlanRecordBean.TROUBLE_RECORD_CLOSE_KEY, mbIsClear);
            bundle.putBoolean(TroublePlanRecordBean.TROUBLE_RECORD_EDITABLE_KEY, mbIsEditable);
            bundle.putSerializable(TroublePlanRecordBean.TROUBLE_RECORD_BEAN_KEY, recordBean);
            Intent intent = new Intent();
            intent.setClass(this, UiTroubleRecordDetail.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        BaseApp.cancelRequest(getClass().getName());
    }

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