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

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.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.MaintainExecuteBean;
import com.tofly.ghrq_route.module.emergencyHandle.ui.adapter.EmergencyRepairHandleAdapter;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


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

    private EmergencyRepairHandleAdapter repairHandleAdapter;

    private EmergencyHandleData mEmergencyData;

    private boolean isEditable;

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

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

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

    private void initView() {
        setDarkStatusBar();

        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_large);
        dividerItemDecoration.setDrawable(drawable);
        recyclerView.addItemDecoration(dividerItemDecoration);
    }

    private void initData() {

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            EmergencyHandleData handleData =
                    (EmergencyHandleData) bundle.getSerializable(EmergencyHandleData.EMERGENCY_DATA_KEY);
            if (handleData != null) {
                mEmergencyData = handleData;
                setAdapter();
            }
        }
    }

    private void setAdapter() {
        if (mEmergencyData != null) {
            List<MaintainExecuteBean> executeList = mEmergencyData.getSchedulerMaintenanceHisList();
            if (repairHandleAdapter == null) {
                repairHandleAdapter = new EmergencyRepairHandleAdapter(this, mEmergencyData, executeList);
                recyclerView.setAdapter(repairHandleAdapter);
            } else {
                repairHandleAdapter.setData(executeList);
            }
        }
    }

    private void initListener() {
        recyclerView.setOnRefreshListener(() -> getOnlineList());
    }

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

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

    private void getOnlineList() {
        startRefreshing();
        setAdapter();
        stopRefreshing();
    }

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

        EventBusUtil.unregister(this);
    }
}
