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.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;

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

public class HFTaskInputDialog extends Dialog {
    private String mTitle;
    private String mMessage;
    private EditText mEditTextMessage;

    private OnDialogCallback mCallback;

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

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

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

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

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

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

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

        setContentView(R.layout.dialog_hf_task_input);

        TextView textViewTitle = (TextView) findViewById(R.id.title_textView);
        TextView textViewPositive = (TextView) findViewById(R.id.positive_textView);
        mEditTextMessage = (EditText) findViewById(R.id.message_editText);

        textViewTitle.setText(mTitle);
        if (TextUtils.isEmpty(mMessage) == false) {
            mEditTextMessage.setText(mMessage);
            mEditTextMessage.setSelection(mMessage.length());
        }

        textViewPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCallback != null) mCallback.onPositive(mEditTextMessage.getText().toString());
                HFTaskInputDialog.this.dismiss();
            }
        });

        findViewById(R.id.close_imageButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HFTaskInputDialog.this.dismiss();
            }
        });
    }

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {
        void onPositive(@NonNull String message);
    }
}
