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.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

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.domain.AuditSchemeBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleProcessBean;
import com.tofly.ghrq_route.module.trouble.ui.UiSchemeVerifyList;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleDutyMakeScheme;
import com.tofly.ghrq_route.module.trouble.ui.adapter.TroubleSchemeVerifyAdapter;
import com.tofly.ghrq_route.module.trouble.ui.dialog.TroubleSchemeDetailBottomDialog;

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

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

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

public class TroubleVerifyMakePlanModuleLayout extends LinearLayout {
    @BindView(R.id.text_under_sectary_name)
    TextView textUnderSectaryName;
    @BindView(R.id.text_verify_time)
    TextView textVerifyTime;
    @BindView(R.id.text_verify_state)
    TextView textVerifyState;
    @BindView(R.id.text_assign_duty)
    TextView textAssignDuty;
    @BindView(R.id.layout_assign_duty)
    LinearLayout layoutAssignDuty;
    @BindView(R.id.text_maintian_name)
    TextView textMaintainName;
    @BindView(R.id.layout_maintain_user)
    LinearLayout layoutMaintainUser;
    @BindView(R.id.layout_verify_note)
    LinearLayout layoutVerifyNote;
    @BindView(R.id.text_verify_note)
    TextView textVerifyNote;
    @BindView(R.id.layout_verify_module)
    LinearLayout layoutVerifyModule;
    @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.btn_action)
    Button btnAction;
    @BindView(R.id.layout_action)
    LinearLayout layoutAction;

    private Context mContext;

    private TroubleBean mTroubleBean;

    private boolean isEditable;

    private TroubleSchemeVerifyAdapter schemeVerifyAdapter;

    private TroubleSchemeDetailBottomDialog schemeDetailBottomDialog;

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

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

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

        mContext = context;

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_trouble_plan_make_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) {
        layoutVerifyModule.setVisibility(GONE);
        if (checkUndertakerVerify()) {
            layoutVerifyModule.setVisibility(VISIBLE);
            layoutMaintainUser.setVisibility(VISIBLE);
            textUnderSectaryName.setText("其他班组长派工");
            textMaintainName.setText(troubleBean.getMaintainerUserName());
        }else if (checkUnderSectaryVerify()) {
            layoutVerifyModule.setVisibility(VISIBLE);
            layoutMaintainUser.setVisibility(GONE);
            textUnderSectaryName.setText("副部长派工");
        }

        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<AuditSchemeBean> schemeBeanList = mTroubleBean.getAuditSchemeList();
        if (schemeBeanList != null && schemeBeanList.size() > 0) {
            layoutCheckReportModule.setVisibility(VISIBLE);
            List<AuditSchemeBean> schemes = new ArrayList<>();
            schemes.add(schemeBeanList.get(0));
            schemeVerifyAdapter = new TroubleSchemeVerifyAdapter(mContext, mTroubleBean, schemes);
            recyclerView.setAdapter(schemeVerifyAdapter);

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

            schemeVerifyAdapter.setOnItemClickListener((view, position) -> {
                AuditSchemeBean schemeBean = schemeVerifyAdapter.getItem(position);
                if (schemeBean != null) {
                    showSchemeDetailDialog(schemeBean);
                }
            });
        }

        setActionButton(troubleBean);
    }

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

        int processId = troubleBean.getProcessId();
        if (processId == TroubleBean.TROUBLE_PROCESS_UNDER_SECTARY_REJECT_SCHEME) {
            layoutAction.setVisibility(VISIBLE);
        } else {
            layoutAction.setVisibility(GONE);
        }
        btnAction.setOnClickListener(view -> goToUiMakeScheme());
    }

    private boolean checkUndertakerVerify() {
        boolean hasModule = false;
        List<TroubleProcessBean> checks = mTroubleBean.getChecks();
        for (TroubleProcessBean check : checks) {
            if (check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_OTHER_UNDERTAKER_MAKE_SCHEME) {
                hasModule = true;
                break;
            }
        }
        return hasModule;
    }

    private boolean checkUnderSectaryVerify() {
        boolean hasModule = false;
        List<TroubleProcessBean> checks = mTroubleBean.getChecks();
        for (TroubleProcessBean check : checks) {
            if (check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_MAKE_SCHEME) {
                hasModule = true;
                break;
            }
        }
        return hasModule;
    }

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

    @OnClick({R.id.layout_check_more})
    public void onClick(View view) {
        if (view.getId() == R.id.layout_check_more) {
            checkMoreList();
        }
    }

    private void goToUiMakeScheme() {
        Intent intent = new Intent();
        intent.setClass(mContext, UiTroubleDutyMakeScheme.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.putInt(TroubleBean.TROUBLE_DETAIL_LIST_TYPE_KEY, 1);
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_EDITABLE_KEY, isEditable);
        bundle.putString(BaseUi.MAIN_TITLE_KEY, mContext.getString(R.string.trouble_scheme_list));
        intent.putExtras(bundle);
        Activity activity = (Activity) mContext;
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }
}
