package com.casic.dcms.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.casic.dcms.R;
import com.casic.dcms.utils.LocaleConstant;
import com.pengxh.androidx.lite.kit.DialogKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.widget.DeleteEditText;

public class CancelAlarmDialog extends Dialog implements View.OnClickListener {

    private static final String TAG = "CancelAlarmDialog";
    private final Context context;
    private final OnDialogButtonClickListener listener;
    private DeleteEditText cancelReasonView;
    private String status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogKit.resetParams(this, 0.85);
        setContentView(R.layout.dialog_cancel_alarm);

        Spinner reasonSpinner = findViewById(R.id.selectReasonView);
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, LocaleConstant.SPINNER_ITEMS);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        reasonSpinner.setAdapter(spinnerAdapter);

        cancelReasonView = findViewById(R.id.cancelReasonView);

        Button dialogCancelButton = findViewById(R.id.dialogCancelButton);
        dialogCancelButton.setOnClickListener(this);

        Button dialogConfirmButton = findViewById(R.id.dialogConfirmButton);
        dialogConfirmButton.setOnClickListener(this);

        reasonSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                status = String.valueOf(position + 2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.dialogCancelButton) {
            if (listener != null) {
                listener.onCancelClick();
            }
        } else if (i == R.id.dialogConfirmButton) {
            if (listener != null) {
                String editReason = cancelReasonView.getText().toString().trim();
                if (status.equals("2")) {
                    StringKit.show(context, "原因还没选，无法取消报警");
                    return;
                }
                if (TextUtils.isEmpty(editReason)) {
                    StringKit.show(context, "理由还没填写，无法取消报警");
                    return;
                }
                listener.onConfirmClick(status, editReason);
            }
        }
        this.dismiss();
    }

    public interface OnDialogButtonClickListener {
        void onConfirmClick(String status, String editReason);

        void onCancelClick();
    }

    private CancelAlarmDialog(Builder builder) {
        super(builder.mContext);
        this.context = builder.mContext;
        this.listener = builder.listener;
    }

    public static class Builder {
        private Context mContext;
        private OnDialogButtonClickListener listener;

        public Builder setContext(Context context) {
            this.mContext = context;
            return this;
        }

        public Builder setOnDialogButtonClickListener(OnDialogButtonClickListener listener) {
            this.listener = listener;
            return this;
        }

        public CancelAlarmDialog build() {
            return new CancelAlarmDialog(this);
        }
    }
}
