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

import android.support.v7.app.ActionBar;
import android.util.Log;

import io.reactivex.disposables.Disposable;
import kr.co.zumo.app.lifeplus.model.PinModel;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.IPinConfirmView;
import kr.co.zumo.app.lifeplus.view.command.ActivityChangeCommand;

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

  private Disposable disposable;

  public PinConfirmPresenter(PinModel model, IPinConfirmView 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 != disposable) {
      disposable.dispose();
      disposable = null;
    }
  }

  @Override
  public void onResult(Event event) {

  }

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

  @Override
  protected void onEventInternal(Event event) {
    switch (event.getEventId()) {
      case Event.RESULT:
        Log.i("APP# PinConfirmPresenter | onEventInternal", "|" + event.getString());
        verifyPin(event.getString());
        break;
      case Event.FAIL:
        onBackPressed();
        break;
      case Event.RESET:
        onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_PIN_RESET, ScreenID.DIRECTION_NEXT));
        break;
      default:
        break;
    }
  }

  /**
   * 핀 검증
   *
   * @param pin
   */
  private void verifyPin(String pin) {

    boolean result = model.verifyPin(pin);

    if (result) {
      // 저장된 핀 번호와 일치 함
      onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_MAIN, ScreenID.DIRECTION_NEXT));
    }
    else {
      // 핀 번호 불 일치
      // view 에 다시 요청함
      view.retry();
    }
  }

  @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) {

  }
}
