/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
package kr.co.zumo.app.lifeplus.view.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.util.StringUtil;
import kr.co.zumo.app.lifeplus.view.Event;

/**
 * AlertDialog
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 23.]   [최초 작성]
 * @since 2018. 9. 23.
 */
public class AlertDialog extends DialogBase {

  private String text;
  private TextView textView;
  private View buttonConfirm;

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

  @Override
  public void onDestroyView() {
    super.onDestroyView();
  }

  /**
   * 표시할 text 지정
   *
   * @param text
   */
  public void setText(String text) {
    this.text = text;

    render();
  }

  private void render() {
    if (null != textView && StringUtil.isFull(text)) {
      textView.setText(text);
    }
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.alert_dialog, container, false);
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    textView = getView().findViewById(R.id.text_alert_message);
    buttonConfirm = getView().findViewById(R.id.button_confirm);
    buttonConfirm.setOnClickListener(v -> {
      if (null != getResultListener()) {
        getResultListener().onDialogResult(AlertDialog.this, new Event.Builder(Event.CONFIRM).build());
      }
    });

    render();

  }

  @Override
  public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
  }

  @Override
  public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);

    if (null != getResultListener()) {
      getResultListener().onDialogCanceled(this);
    }
  }
}
