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

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.common.ui.view.MyAudioRecordButton;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class EmergencyAssignHandleReportModuleLayout extends LinearLayout {
    @BindView(R.id.layout_over_time)
    LinearLayout layoutOverTime;
    @BindView(R.id.text_over_time)
    TextView textOverTime;
    @BindView(R.id.radio_group_repair)
    RadioGroup radioGroupRepair;
    @BindView(R.id.radio_group_verify)
    RadioGroup radioGroupVerify;
    @BindView(R.id.edit_handle_note)
    EditText editHandleNote;
    @BindView(R.id.layout_sign_module)
    LinearLayout layoutSignModule;
    @BindView(R.id.btn_add_signature)
    Button btnAddSign;
    @BindView(R.id.sign_gallery)
    LinearLayout signGallery;
    @BindView(R.id.btn_add_photo)
    Button btnAddPhoto;
    @BindView(R.id.photo_gallery)
    LinearLayout photoGallery;
    @BindView(R.id.btn_audio_record)
    MyAudioRecordButton btnAudioRecord;

    private Context mContext;

    private boolean isEditable;

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

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

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

        mContext = context;

        initView();
        initListener();
    }

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

    private void initListener() {
        radioGroupRepair.setOnCheckedChangeListener((radioGroup, i) -> {
            boolean isNeedRepair = radioGroup.getCheckedRadioButtonId() == R.id.radio_btn_yes;
            layoutSignModule.setVisibility(isNeedRepair ? GONE : VISIBLE);
        });
    }

    public void initEmergencyInfo(EmergencyHandleData emergencyData) {
        if (emergencyData != null) {
            setViewContent(emergencyData);
        }
    }

    private void setViewContent(EmergencyHandleData emergencyData) {
        /*到位时间*/
        String signTime = emergencyData.getSignTime();
        if (signTime != null && !signTime.isEmpty()) {
            layoutOverTime.setVisibility(VISIBLE);
            textOverTime.setText(signTime);
        }
    }

    public boolean isValid() {
        if (editHandleNote.getText().toString().isEmpty()) {
            editHandleNote.requestFocus();
            ToastUtil.showToast(mContext, mContext.getString(R.string.emergency_assign_handle_empty_note));
            return false;
        }

        return true;
    }

    public void collectData(EmergencyHandleData handleData) {
        handleData.setIsMaintenance(radioGroupRepair.getCheckedRadioButtonId() == R.id.radio_btn_yes ? 1 : 0);
        handleData.setAuditFlag(radioGroupVerify.getCheckedRadioButtonId() == R.id.radio_verify_yes ? 1 : 0);

        String note = editHandleNote.getText().toString();
        handleData.setEmergencyHandling(note);
    }
}
