package com.tofly.ghrq_route.module.trouble.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.trouble.ui.dialog.TroubleExecuteDetailBottomDialog;
import com.tofly.ghrq_route.module.trouble.ui.dialog.TroubleSchemeDetailBottomDialog;
import com.tofly.ghrq_route.module.trouble.domain.AuditEndBean;
import com.tofly.ghrq_route.module.trouble.domain.AuditSchemeBean;
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.adapter.TroubleExecuteReportAdapter;
import com.tofly.ghrq_route.module.trouble.ui.adapter.TroubleSchemeVerifyAdapter;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


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

    private TroubleSchemeVerifyAdapter schemeVerifyAdapter;

    private TroubleExecuteReportAdapter executeReportAdapter;

    private int mListType = 1;

    private TroubleBean mTroubleBean;

    private boolean isEditable;

    private TroubleSchemeDetailBottomDialog schemeDetailBottomDialog;

    private TroubleExecuteDetailBottomDialog executeDetailBottomDialog;

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

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

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

        EventBusUtil.register(this);
    }

    @Subscribe
    public void onMessageEvent(MyTroubleEvent event) {
        if (MyTroubleEvent.TROUBLE_UPLOADED.equals(event.message) ||
                MyTroubleEvent.TROUBLE_SAVED.equals(event.message)) {
            finish();
        }
    }

    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) {
            mTroubleBean = (TroubleBean) bundle.getSerializable(TroubleBean.TROUBLE_BEAN_KEY);
            isEditable = bundle.getBoolean(TroubleBean.TROUBLE_BEAN_EDITABLE_KEY, true);
            mListType = bundle.getInt(TroubleBean.TROUBLE_DETAIL_LIST_TYPE_KEY, 1);
            if (mTroubleBean != null) {
                setAdapter();
            }
        }
    }

    private void setAdapter() {
        if (mTroubleBean != null) {
            if (mListType == 2) {
                List<AuditEndBean> auditEndList = mTroubleBean.getAuditEndList();
                if (executeReportAdapter == null) {
                    executeReportAdapter = new TroubleExecuteReportAdapter(this, mTroubleBean, auditEndList);
                    recyclerView.setAdapter(executeReportAdapter);
                }else {
                    executeReportAdapter.setData(auditEndList);
                }
            } else {
                List<AuditSchemeBean> auditSchemeList = mTroubleBean.getAuditSchemeList();
                if (schemeVerifyAdapter == null) {
                    schemeVerifyAdapter = new TroubleSchemeVerifyAdapter(this, mTroubleBean, auditSchemeList);
                    recyclerView.setAdapter(schemeVerifyAdapter);
                } else {
                    schemeVerifyAdapter.setData(auditSchemeList);
                }
            }
        }
    }

    private void initListener() {
        recyclerView.setOnRefreshListener(this::getOnlineList);

        if (mListType == 2) {
            executeReportAdapter.setOnItemClickListener((view, position) -> {
                AuditEndBean auditEndBean = executeReportAdapter.getItem(position);
                if (auditEndBean != null) {
                    showExecuteDetailDialog(auditEndBean);
                }
            });
        }else {
            schemeVerifyAdapter.setOnItemClickListener((view, position) -> {
                AuditSchemeBean schemeBean = schemeVerifyAdapter.getItem(position);
                if (schemeBean != null) {
                    showSchemeDetailDialog(schemeBean);
                }
            });
        }
    }

    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();
    }


    private void showExecuteDetailDialog(AuditEndBean auditEndBean) {
        if (executeDetailBottomDialog == null) {
            executeDetailBottomDialog = new TroubleExecuteDetailBottomDialog(this, mTroubleBean, isEditable, auditEndBean);
        }else  {
            executeDetailBottomDialog.setContentView(auditEndBean);
        }
        if (!executeDetailBottomDialog.isShowing()) {
            executeDetailBottomDialog.show();
        }
    }

    private void showSchemeDetailDialog(AuditSchemeBean schemeBean) {
        if (schemeDetailBottomDialog == null) {
            schemeDetailBottomDialog = new TroubleSchemeDetailBottomDialog(this, mTroubleBean, isEditable, schemeBean);
        } else  {
            schemeDetailBottomDialog.setContentView(schemeBean);
        }
        if (!schemeDetailBottomDialog.isShowing()) {
            schemeDetailBottomDialog.show();
        }
    }

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

        EventBusUtil.unregister(this);
    }
}
