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

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.IPinView;
import kr.co.zumo.app.lifeplus.view.command.ActivityChangeCommand;

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

  private Disposable disposable;

  public PinPresenter(PinModel model, IPinView view) {
    super(model, view);
  }

  @Override
  public void start() {

  }

  @Override
  protected void onDispose() {
    if (null != disposable) {
      disposable.dispose();
      disposable = null;
    }
  }

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

  @Override
  protected void performOnEvent(Event event) {
    switch (event.getEventId()) {
      default:
        break;
    }
  }

  /**
   * pin 검증 수를 초기화 한다.
   */
  public void resetCounter() {
    model.resetCount();
  }

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

    view.setDisabledPinDialog();
    boolean result = model.verifyPin(pin);

    if (result) {
      // 검증 완료
      view.dismissPinDialog();
      onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_MAIN, ScreenID.DIRECTION_NEXT));

    }
    else {
      // 검증 실패, 재입력
      countTrying(model, view);

    }


//    disposable = model.verifyPin(SuperModel.getInstance().getDeviceUuid(), pin)
//      .observeOn(AndroidSchedulers.mainThread())
//      .subscribe(bean -> {
//
//        Log.i("APP# PinPresenter | verifyPin", "|" + bean.toPrettyJson());
//
//        // todo 검증 학인 로직 필요
//        if (StringUtil.isFull(bean.getReturnMessage())) {
//          // 검증 완료
//          view.dismissPinDialog();
//          onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_MAIN, ScreenID.DIRECTION_NEXT));
//        }
//        else {
//          // 검증 실패, 재입력
//          countTrying(model, view);
//        }
//      }, e -> {
//        Log.e("APP# PinPresenter | verifyPin", "|" + e.getLocalizedMessage());
//
//        if (e instanceof EOFException) {
//          Log.e("APP#  PinPresenter | verifyPin", "| EOFException-- " + e.getLocalizedMessage());
//        }
//        else if (e instanceof SocketTimeoutException) {
//          Log.e("APP#  PinPresenter | verifyPin", "| SocketTimeoutException-- " + e.getLocalizedMessage());
//        }
//
//        // fixme 에러 종류에 따라서 처리 필요
//        // 통신 오류, 파싱 오류 등
//        countTrying(model, view);
//      });
  }

  private void countTrying(PinModel model, IPinView view) {
    model.countTrying();
    final int count = model.getCount();
    int maxCount = model.getMaxTrying();

    Log.i("APP# PinPresenter | count", "| current: " + model.getCount() + ", Max: " + maxCount);

    if (count < maxCount) {
      view.restartDialog();
    }
    else {
      view.dismissPinDialog();

      // 5회 실패 본인 인증 화면으로 이동
      onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_AUTHORIZATION, ScreenID.DIRECTION_NEXT));
    }
  }
}
