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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

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.common.util.date.DateUtils;
import com.tofly.ghrq_route.module.trouble.domain.AuditEndBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.ui.UiSchemeVerifyList;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleDutyHandleScheme;
import com.tofly.ghrq_route.module.trouble.ui.adapter.TroubleExecuteReportAdapter;
import com.tofly.ghrq_route.module.trouble.ui.dialog.TroubleExecuteDetailBottomDialog;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/21.
 */

public class TroubleExecuteReportModuleLayout extends LinearLayout {
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;
    @BindView(R.id.layout_check_more)
    LinearLayout layoutCheckMore;
    @BindView(R.id.layout_check_report_module)
    LinearLayout layoutCheckReportModule;
    @BindView(R.id.layout_action)
    LinearLayout layoutAction;
    private Context mContext;

    private TroubleBean mTroubleBean;

    private boolean isEditable;

    private TroubleExecuteReportAdapter executeReportAdapter;

    private TroubleExecuteDetailBottomDialog executeDetailBottomDialog;

    public TroubleExecuteReportModuleLayout(Context context) {
        this(context, null);
    }

    public TroubleExecuteReportModuleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TroubleExecuteReportModuleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_trouble_execute_report_verify, this);
        ButterKnife.bind(this);
    }

    private void initListener() {

    }

    public void initTroubleInfo(TroubleBean troubleBean, boolean isEditable) {
        if (troubleBean != null) {
            mTroubleBean = troubleBean;
            this.isEditable = isEditable;
            setViewContent(troubleBean);
        }
    }

    private void setViewContent(TroubleBean troubleBean) {
        layoutCheckReportModule.setVisibility(GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.bg_recyclerview_divider_small);
        dividerItemDecoration.setDrawable(drawable);
        recyclerView.addItemDecoration(dividerItemDecoration);

        List<AuditEndBean> auditEndList = mTroubleBean.getAuditEndList();
        Collections.sort(auditEndList, (o1, o2) -> -DateUtils.getTimeCompare(o1.getHandleTime(), o2.getHandleTime()));
        if (auditEndList != null && auditEndList.size() > 0) {
            layoutCheckReportModule.setVisibility(VISIBLE);
            List<AuditEndBean> auditEndBeans = new ArrayList<>();
            auditEndBeans.add(auditEndList.get(0));
            executeReportAdapter = new TroubleExecuteReportAdapter(mContext, mTroubleBean, auditEndBeans);
            recyclerView.setAdapter(executeReportAdapter);

            layoutCheckMore.setVisibility(auditEndList.size() > 1 ? VISIBLE : GONE);

            executeReportAdapter.setOnItemClickListener((view, position) -> {
                AuditEndBean auditEndBean = executeReportAdapter.getItem(position);
                if (auditEndBean != null) {
                    showSchemeDetailDialog(auditEndBean);
                }
            });
        }

        setActionButton(troubleBean);
    }

    private void setActionButton(TroubleBean troubleBean) {
        if (!isEditable) {
            layoutAction.setVisibility(GONE);
            return;
        }

        boolean needExecute = checkNeedExecute(troubleBean);
        layoutAction.setVisibility(needExecute ? VISIBLE : GONE);
    }

    private boolean checkNeedExecute(TroubleBean troubleBean) {
        boolean needExecute = false;
        int processId = troubleBean.getProcessId();
        for (int process : TroubleBean.TROUBLE_JOB_EXECUTE_PROCESS) {
            if (processId == process) {
                needExecute = true;
                break;
            }
        }

        return needExecute;
    }

    private void showSchemeDetailDialog(AuditEndBean auditEndBean) {
        if (executeDetailBottomDialog == null) {
            executeDetailBottomDialog = new TroubleExecuteDetailBottomDialog(mContext, mTroubleBean, isEditable, auditEndBean);
        }
        if (!executeDetailBottomDialog.isShowing()) {
            executeDetailBottomDialog.show();
        }
    }

    @OnClick({R.id.layout_check_more, R.id.btn_action})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_check_more:
                checkMoreList();
                break;
            case R.id.btn_action:
                goToUiDutyHandleScheme();
                break;
        }
    }

    private void goToUiDutyHandleScheme() {
        Intent intent = new Intent();
        intent.setClass(mContext, UiTroubleDutyHandleScheme.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, mTroubleBean);
        intent.putExtras(bundle);
        Activity activity = (Activity) mContext;
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }
    private void checkMoreList() {
        Intent intent = new Intent();
        intent.setClass(mContext, UiSchemeVerifyList.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, mTroubleBean);
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_EDITABLE_KEY, isEditable);
        bundle.putInt(TroubleBean.TROUBLE_DETAIL_LIST_TYPE_KEY, 2);
        bundle.putString(BaseUi.MAIN_TITLE_KEY, mContext.getString(R.string.trouble_execute_list));
        intent.putExtras(bundle);
        Activity activity = (Activity) mContext;
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }
}
