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

import java.security.NoSuchAlgorithmException;

import kr.co.zumo.app.lifeplus.util.StringUtil;

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

  private int countOfTrying;
  private final static int MAX_TRYING = 5;

  /**
   * 핀 입력 시도 수 증가 시킴
   */
  public void countTrying() {
    ++countOfTrying;
  }

  /**
   * 시도 수 반환
   *
   * @return countOfTrying
   */
  public int getCount() {
    return countOfTrying;
  }

  /**
   * 검증 수 리셋
   */
  public void resetCount() {
    countOfTrying = 0;
  }

  public int getMaxTrying() {
    return MAX_TRYING;
  }

  /**
   * 입력한 핀과 저장된 핀이 같은지 확인
   *
   * @param pin "0000"
   * @return boolean
   */
  public boolean verifyPin(String pin) {
    String encryptedPin;
    try {
      encryptedPin = StringUtil.convertToSHA256(StringUtil.convertToMD5(pin));
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      encryptedPin = "";
    }
    String savedPin = SuperModel.getInstance().getPreferences().getEncryptedPin();

    return StringUtil.isFull(encryptedPin) && StringUtil.isFull(savedPin) && savedPin.equals(encryptedPin);
  }

  @Override
  public void dispose() {

  }
}
