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

import android.util.Log;

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

/**
 * 앱에 필요한 전반적인 데이터 저장
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 5.]   [최초 작성]
 * @since 2018. 9. 5.
 */
public final class SuperModel {

  public static final int MEMBER_STATUS_BIT_BLOCK = 0;  // 잠김
  public static final int MEMBER_STATUS_BIT_REST = 1;   // 휴면
  public static final int MEMBER_STATUS_BIT_OUT = 2;    // 탈퇴

  private static final SuperModel ourInstance = new SuperModel();

  public static SuperModel getInstance() {
    return ourInstance;
  }

  private String deviceUuid;
  private String UserId;
  private String token;
  private int memberStatus = 0;
  private LifeplusPreferences preferences;

  private SuperModel() {
  }

  /**
   * 초기화. SplashActivity 에서 호출된다.
   *
   * @param preferences LifeplusPreferences
   */
  public void init(LifeplusPreferences preferences) {
    this.preferences = preferences;
  }

  /**
   * dispose
   */
  public void dispose() {
  }

  /**
   * preferences 객체 반환
   *
   * @return LifeplusPreferences 객체
   */
  public LifeplusPreferences getPreferences() {
    return preferences;
  }

  /**
   * 저장된 장치 고유번호 반환
   * - 프리퍼런스에 저장되어있지만 편의를 위해 모델에도 저장해둠
   *
   * @return uuid
   */
  public String getDeviceUuid() {
    if (StringUtil.isEmpty(deviceUuid)) {
      deviceUuid = preferences.getDeviceUuid();
    }
    return deviceUuid;
  }

  /**
   * 장치 고유번호 저장;
   *
   * @param deviceId
   */
  public void setDeviceId(String deviceId) {
    this.deviceUuid = deviceId;
    preferences.setDeviceUuid(deviceId);
  }

  /**
   * 저장된 장치 User ID 반환
   * - 프리퍼런스에 저장되어있지만 편의를 위해 모델에도 저장해둠
   *
   * @return user id
   */
  public String getUserId() {
    if (StringUtil.isEmpty(UserId)) {
      UserId = preferences.getUserId();
    }
    return UserId;
  }

  public void setUserId(String userId) {
    UserId = userId;
    preferences.setUserId(userId);
  }

  /**
   * 유저 토큰
   *
   * @return
   */
  public String getToken() {
    if (null == token) {
      token = preferences.getToken();
    }
    return token;
  }

  public void setToken(String token) {
    this.token = token;
    preferences.setToken(token);
  }

  /**
   * 회원 상태 추가
   * - 잠긴 회원|휴면 회원 등
   *
   * @param memberStatusBit
   */
  public void addMemberStatus(int memberStatusBit) {
    // bit 연산
    this.memberStatus = memberStatus | (1 << memberStatusBit);
  }

  public void deleteMemberStatus(int memberStatusBit) {
    // bit 연산
    this.memberStatus = memberStatus & ~(1 << memberStatusBit);
  }

  public int getMemberStatus() {
    return memberStatus;
  }

  /**
   * 활성 상태의 회원인지 판단.
   * - 잠긴, 휴면 등으로 활성 상태가 아닌 경우 비회원으로 진행
   *
   * @return
   */
  public boolean isActiveMember() {
    return StringUtil.isFull(getToken()) && StringUtil.isFull(getUserId()) && memberStatus == 0;
  }

  /**
   * 블럭 멥버 확인
   *
   * @return
   */
  public boolean isBlockMember() {
    return ((memberStatus >> MEMBER_STATUS_BIT_BLOCK) & 0b01) == 1;
  }

  /**
   * 휴면 멤버 확인
   *
   * @return
   */
  public boolean isRestMember() {
    return ((memberStatus >> MEMBER_STATUS_BIT_REST) & 0b01) == 1;
  }

  /**
   * 휴면 멤버 확인
   *
   * @return
   */
  public boolean isOutMember() {
    return ((memberStatus >> MEMBER_STATUS_BIT_OUT) & 0b01) == 1;
  }

  /**
   * 핀 저장
   *
   * @param pin
   */
  public void savePinWithEncryption(String pin) {
    try {
      String encryptedPin = StringUtil.convertToSHA256(StringUtil.convertToMD5(pin));
      Log.i("APP# SuperModel | savePinWithEncryption", "| pin: " + pin + ", encryptedPin: " + encryptedPin);
      getPreferences().setEncryptedPin(encryptedPin);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
