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

import android.support.v7.app.ActionBar;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.model.RestMemberModel;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.util.ResourceUtil;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.IView;
import kr.co.zumo.app.lifeplus.view.command.ActivityChangeCommand;
import kr.co.zumo.app.lifeplus.view.command.EmailSendingCommand;
import kr.co.zumo.app.lifeplus.view.command.FragmentChangeCommand;
import kr.co.zumo.app.lifeplus.view.dialog.AlertDialog;
import kr.co.zumo.app.lifeplus.view.dialog.DialogBase;
import kr.co.zumo.app.lifeplus.view.dialog.DialogBuilder;
import kr.co.zumo.app.lifeplus.view.dialog.DialogID;
import kr.co.zumo.app.lifeplus.view.dialog.IAttribute;
import kr.co.zumo.app.lifeplus.view.dialog.IDialogResultListener;
import kr.co.zumo.app.lifeplus.view.presenter.Presenter;

/**
 * RestMemberStartPresenter
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 10. 8.]   [최초 작성]
 * @since 2018. 10. 8.
 */
public class RestMemberStartPresenter extends Presenter<RestMemberModel, IView> {

  DialogBase alertDialog;

  public RestMemberStartPresenter(RestMemberModel model, IView view) {
    super(model, view);
  }

  @Override
  public boolean onBackPressed() {
    onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_MAIN, ScreenID.DIRECTION_BACK));
    return true;
  }

  @Override
  protected void destroyInternal() {
    if (null != alertDialog) {
      alertDialog.dispose();
      alertDialog = null;
    }
  }

  @Override
  protected void startInternal() {

  }

  @Override
  protected void stopInternal() {

  }

  @Override
  protected void resumeInternal() {

  }

  @Override
  protected void pauseInternal() {

  }

  @Override
  protected void onEventInternal(Event event) {
    switch (event.getEventId()) {
      case Event.EXIT:
        onBackPressed();
        break;
      case Event.CONFIRM:
        // 휴면 해제 api
        model.activeMember();
        break;
      case Event.EMAIL_CLICK:
        onCommand(new EmailSendingCommand(ResourceUtil.getString(R.string.lifeplus_email)));
        break;
      default:
        break;
    }

  }

  @Override
  public void onResult(Event event) {
    switch (event.getEventId()) {
      case Event.SUCCESS:
        onCommand(new FragmentChangeCommand(ScreenID.REST_MEMBER_ACTIVE_COMPLETED, ScreenID.DIRECTION_NEXT));
        break;
      case Event.ERROR:
        showErrorPopup(event.getString());
        break;
      default:
        break;
    }
  }

  private void showErrorPopup(String errorMessage) {
    alertDialog = DialogBuilder.create(DialogID.ALERT)
      .listener(new IDialogResultListener() {
        @Override
        public void onDialogResult(DialogBase dialog, Event event) {
          if (event.getEventId() == Event.CONFIRM) {
            dialog.dispose();
          }
        }

        @Override
        public void onDialogCanceled(DialogBase dialog) {
          dialog.dispose();
        }
      })
      .attribute((IAttribute<AlertDialog>) dialog -> {
        dialog.setText(errorMessage);
      })
      .show();
  }

  @Override
  public void onClickTitle(ActionBar actionBar) {

  }

  @Override
  public void onClickSearch(ActionBar actionBar) {

  }

  @Override
  public void onClickMenu(ActionBar actionBar) {

  }

  @Override
  public void onClickLike(ActionBar actionBar) {

  }

  @Override
  public void onClickBookmark(ActionBar actionBar) {

  }

  @Override
  public void onClickShare(ActionBar actionBar) {

  }
}
