package kr.co.hanwha.hifive.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.enums.AlertType;

/**
 * Created by sanghoonkwon on 2018. 1. 10..
 */

public class HFDefaultDialog extends Dialog {
    private String mTitle;
    private String mMessage;
    private String mNegative;
    private String mPositive;
    private AlertType mAlertType;
    ;

    private OnDialogCallback mCallback;

    public HFDefaultDialog(@NonNull Context context) {
        super(context);
    }

    public HFDefaultDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected HFDefaultDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public HFDefaultDialog(@NonNull Context context, @NonNull String title, @NonNull String message,
                           String negative, String positive, AlertType alertType, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(false);
        this.mTitle = title;
        this.mMessage = message;
        this.mNegative = negative;
        this.mPositive = positive;
        this.mAlertType = alertType;
        this.mCallback = callback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_hf_default);

        TextView textViewTitle = (TextView) findViewById(R.id.title_textView);
        TextView textViewMessage = (TextView) findViewById(R.id.message_textView);
        TextView textViewNegative = (TextView) findViewById(R.id.negative_textView);
        TextView textViewPositive = (TextView) findViewById(R.id.positive_textView);
        TextView textViewPositiveTypeB = (TextView) findViewById(R.id.positiveTypeB_textView);

        if (TextUtils.isEmpty(mTitle)) {
            textViewTitle.setVisibility(View.GONE);
        } else {
            textViewTitle.setVisibility(View.VISIBLE);
            textViewTitle.setText(mTitle);
        }

        if (mAlertType == AlertType.TYPE_B) {
            textViewPositive.setVisibility(View.GONE);
            textViewPositiveTypeB.setVisibility(View.VISIBLE);
        } else {
            textViewPositive.setVisibility(View.VISIBLE);
            textViewPositiveTypeB.setVisibility(View.GONE);
        }

        textViewMessage.setText(mMessage);
        textViewNegative.setText(mNegative);
        textViewPositive.setText(mPositive);
        textViewPositiveTypeB.setText(mPositive);

        textViewNegative.setOnClickListener(onClickListener);
        textViewPositive.setOnClickListener(onClickListener);
        textViewPositiveTypeB.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mCallback != null) {
                switch (view.getId()) {
                    case R.id.negative_textView:
                        if (mCallback != null) mCallback.onNegative();
                        HFDefaultDialog.this.dismiss();
                        break;
                    case R.id.positive_textView:
                    case R.id.positiveTypeB_textView:
                        if (mCallback != null) mCallback.onPositive();
                        HFDefaultDialog.this.dismiss();
                        break;
                }
            }
        }
    };

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {
        void onNegative();

        void onPositive();
    }
}
