package kr.co.hanwha.hifive.dialog;

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

import kr.co.hanwha.hifive.R;

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

public class HFSingleDialog extends Dialog {
    private String mTitle;
    private String mMessage;
    private String mPositive;

    private OnDialogCallback mCallback;

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

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

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

    public HFSingleDialog(@NonNull Context context, @NonNull String title, @NonNull String message,
                          String positive) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(false);
        this.mTitle = title;
        this.mMessage = message;
        this.mPositive = positive;
    }

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

    public HFSingleDialog(@NonNull Context context, @NonNull int titleRes, @NonNull int messageRes,
                          int positiveRes, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(false);
        this.mTitle = context.getString(titleRes);
        this.mMessage = context.getString(messageRes);
        this.mPositive = context.getString(positiveRes);
        this.mCallback = callback;
    }

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

        setContentView(R.layout.dialog_hf_single);

        TextView textViewTitle = (TextView) findViewById(R.id.title_textView);
        TextView textViewMessage = (TextView) findViewById(R.id.message_textView);
        TextView textViewPositive = (TextView) findViewById(R.id.positive_textView);

        textViewTitle.setText(mTitle);
        textViewMessage.setText(mMessage);
        try {
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
                textViewMessage.setText(Html.fromHtml(mMessage));
            } else {
                textViewMessage.setText(Html.fromHtml(mMessage, Html.FROM_HTML_MODE_LEGACY));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        textViewPositive.setText(mPositive);
        textViewPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCallback != null) mCallback.onPositive();
                HFSingleDialog.this.dismiss();
            }
        });
    }

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {
        void onPositive();
    }
}
