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

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
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.module.trouble.TroubleTool;
import com.tofly.ghrq_route.module.trouble.controller.TroubleCallBack;
import com.tofly.ghrq_route.module.trouble.domain.MyTroubleEvent;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleDetail;
import com.tofly.ghrq_route.module.trouble.ui.adapter.TroubleListAdapter;
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;

public class UiMyTroubleList extends BaseUi {
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;

    private TroubleListAdapter troubleListAdapter;

    private TroubleTool troubleTool;

    private int pageindex = 1;

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

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

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

        EventBusUtil.register(this);
    }

    private void initView() {
        setDarkStatusBar();
    }


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

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg_recyclerview_divider);
        if (drawable != null)
            dividerItemDecoration.setDrawable(drawable);
        recyclerView.addItemDecoration(dividerItemDecoration);

        List<TroubleBean> troubleBeans = new ArrayList<>();
        troubleListAdapter = new TroubleListAdapter(this, troubleBeans);
        recyclerView.setAdapter(troubleListAdapter);

        getOnlineTroubleList();
    }

    private void initListener() {
        recyclerView.setOnRefreshListener(() -> {
            pageindex = 1;
            getOnlineTroubleList();
        });

        recyclerView.setOnLoadMoreListener(() -> {
            pageindex++;
            getOnlineTroubleList();
        });

        if (troubleListAdapter != null) {
            troubleListAdapter.setOnItemClickListener((view, position) -> {
                TroubleBean troubleBean = troubleListAdapter.getItem(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, troubleBean);
                bundle.putSerializable(TroubleBean.TROUBLE_POSITION_KEY, position);
                bundle.putBoolean(TroubleBean.TROUBLE_BEAN_EDITABLE_KEY, false);
                intent.setClass(UiMyTroubleList.this, UiTroubleDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            });
        }
    }

    @Subscribe
    public void onMessageEvent(MyTroubleEvent event) {
        if (MyTroubleEvent.TROUBLE_TROUBLE_HAS_READ.equals(event.message)) {
            if (event.arg0 != null) {
                int position = (int) event.arg0;
                if (troubleListAdapter != null) {
                    troubleListAdapter.notifyItemChanged(position);
                }
            }
        }
    }

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

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

    private void getOnlineTroubleList() {
        startRefreshing();
        troubleTool.getTroubleListOnline(this, TroubleBean.TROUBLE_SELECT_MY_TROUBLE, "", pageindex, new TroubleCallBack() {
            @Override
            public void onGetTroubleList(boolean isSuccess, int errorCode, String errMsg, List<TroubleBean> troubleList, int total) {
                super.onGetTroubleList(isSuccess, errorCode, errMsg, troubleList, total);
                stopRefreshing();
                if (pageindex == 1) {
                    recyclerView.setTotal(total);
                    troubleListAdapter.clear();
                }
                if (troubleList != null) {
                    troubleListAdapter.addAll(troubleList);
                }
            }
        });
    }

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