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

import android.support.v7.app.ActionBar;

import kr.co.zumo.app.lifeplus.model.MainModel;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.IMainView;
import kr.co.zumo.app.lifeplus.view.command.ActivityChangeCommand;
import kr.co.zumo.app.lifeplus.view.command.ExitCommand;
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;

/**
 * MainPresenter
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 21.]   [최초 작성]
 * @since 2018. 9. 21.
 */
public class MainPresenter extends Presenter<MainModel, IMainView> {

  DialogBase alertDialog;

  public MainPresenter(MainModel model, IMainView view) {
    super(model, view);
  }

  @Override
  protected void startInternal() {
  }

  @Override
  protected void stopInternal() {

  }

  @Override
  protected void resumeInternal() {

  }

  @Override
  protected void pauseInternal() {

  }

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

  @Override
  public void onResult(Event event) {

    switch (event.getEventId()) {
      case Event.COMPLETED:
        if (event.getInteger() == Event.MEMBER_DELETE) {
          showAlert("탈퇴 완료");
        }
        break;
      case Event.ERROR:
        if (event.getInteger() == Event.MEMBER_DELETE) {
          showAlert("탈퇴 에러 발생 - " + event.getString());
        }
        break;
      default:
        break;
    }
  }

  @Override
  public boolean onBackPressed() {
    /*
     첫 화면에서는 종료;
     */
    onCommand(new ExitCommand());
    return true;
  }

  @Override
  protected void onEventInternal(Event event) {
    switch (event.getEventId()) {
      case Event.SIGN_UP:
        int screenId = model.isUpdateUser() ? ScreenID.ACTIVITY_SIGN_UP_FROM_ZUMO : ScreenID.ACTIVITY_SIGN_UP;
        onCommand(new ActivityChangeCommand(screenId, ScreenID.DIRECTION_NEXT));
        break;
      case Event.FAQ:
        onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_FAQ, ScreenID.DIRECTION_NEXT));
        break;
      case Event.INPUT_PIN:
        onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_PIN, ScreenID.DIRECTION_NEXT));
        break;
      case Event.RESET:
        onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_PIN_RESET, ScreenID.DIRECTION_NEXT));
        break;
      case Event.DELETE:
        model.deleteAccount();
        break;
      default:
        break;
    }
  }

  private void showAlert(String str) {

    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) {
        }
      })
      .attribute((IAttribute<AlertDialog>) dialog -> {
        dialog.setText(str);
      })
      .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) {

  }
}
