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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleDutyMakeScheme;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class TroubleUndertakerVerifyModuleLayout 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.layout_verify_note)
    LinearLayout layoutVerifyNote;
    @BindView(R.id.text_verify_note)
    TextView textVerifyNote;
    @BindView(R.id.btn_action)
    Button btnAction;
    @BindView(R.id.layout_action)
    LinearLayout layoutAction;

    private Context mContext;

    private TroubleBean mTroubleBean;

    private boolean isEditable;

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

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

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

        mContext = context;

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_trouble_undertaker_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) {
        textUnderSectaryName.setText(troubleBean.getUndertakerUserName());
        String verify_time = troubleBean.getUndersecretaryCheckTime();
        if (verify_time != null)
            textVerifyTime.setText(verify_time);
        String state_name = troubleBean. getUndersecretaryStateName();
        if (state_name != null)
            textVerifyState.setText(state_name);

        layoutVerifyNote.setVisibility(GONE);
        String checkNotes = troubleBean.getUndersecretaryCheckNotes();
        if (checkNotes != null) {
            layoutVerifyNote.setVisibility(VISIBLE);
            textVerifyNote.setText(checkNotes);
        }

        setActionButton(troubleBean);
    }

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

        int processId = troubleBean.getProcessId();
        if (processId == TroubleBean.TROUBLE_PROCESS_NEED_OTHER_UNDERTAKER_MAKE_SCHEME) {
            layoutAction.setVisibility(VISIBLE);
        }else if (processId == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_MAKE_SCHEME) {
            layoutAction.setVisibility(VISIBLE);
        }else {
            layoutAction.setVisibility(GONE);
        }

        btnAction.setOnClickListener(view -> goToUiMakeScheme());
    }

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