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

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import kr.co.zumo.app.lifeplus.model.LifeplusPreferences;
import kr.co.zumo.app.lifeplus.model.SuperModel;
import kr.co.zumo.app.lifeplus.util.AppUtil;
import kr.co.zumo.app.lifeplus.util.StringUtil;
import kr.co.zumo.app.lifeplus.view.Event;
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;

/**
 * ActivityStarter
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018-09-29]   [최초 작성]
 * @since 2018-09-29
 */
public class ActivityStarter {
  private Context context;

  public ActivityStarter(Context context) {
    this.context = context;
  }

  public void launch(boolean isConnected) {
    Log.i("APP# SplashActivity | onCreate", "| Network Connected ==============================================================> " + isConnected);
    LifeplusPreferences preferences = SuperModel.getInstance().getPreferences();
    Intent intent;
    intent = new Intent(context, MainActivity.class);
    if (isConnected == false) {
      intent = new Intent(context, NetworkErrorActivity.class);
    }
    else if (preferences.isPermissionInfoDone() == false) {
      intent = new Intent(context, PermissionInfoActivity.class);
    }
    else if (preferences.isTutorialDone() == false) {
      intent = new Intent(context, TutorialActivity.class);
    }
    else if (StringUtil.isFull(SuperModel.getInstance().getUserId()) == false) {
      // 처음 사용자와 업데이트 사용자 구분

      // 업데이트 사용자
      if (StringUtil.isFull(preferences.getEncryptionUserNo())) {
        // todo 앱 업데이트 한 사람일 경우
        intent = new Intent(context, SignUpZumoUserActivity.class);
      }
      else { // 처음 사용자
        intent = new Intent(context, SignUpActivity.class);
      }
    }

    try {
      verifyUuid();
    } catch (Throwable e) {
      e.printStackTrace();
      // uuid 가 변경됨, 다시 인증 필요
      intent = null;
      showUuidDialog();
    } finally {
      if (null != intent) {
        context.startActivity(intent);
      }
    }
  }

  private void showUuidDialog() {
    DialogBuilder.create(DialogID.ALERT)
      .listener(new IDialogResultListener() {
        @Override
        public void onDialogResult(DialogBase dialog, Event event) {
          Intent intent = new Intent(context, SignUpActivity.class);
          context.startActivity(intent);
        }

        @Override
        public void onDialogCanceled(DialogBase dialog) {
          Intent intent = new Intent(context, SignUpActivity.class);
          context.startActivity(intent);
        }
      })
      .attribute((IAttribute<AlertDialog>) dialog -> {
        dialog.setText("UUID 가 변경되어 본인 인증이 필요합니다.");
      })
      .show();
  }

  private void verifyUuid() {
    String uuid = SuperModel.getInstance().getPreferences().getDeviceUuid();
    String currentUuid = AppUtil.getDeviceUuid(context);

    if (StringUtil.isFull(uuid)) {
      // 현재 장치 값과 저장된 값이 같은지 검증;
      if (uuid.equals(currentUuid)) {
        // 이상 없음
        Log.w("APP# SplashActivity | verifyUuid", "|" + "이상 없음");
      }
      else {
        Log.e("APP#  SplashActivity | verifyUuid", "| " + "uuid 가 변경됨");
        throw new RuntimeException("uuid changed.");
      }
    }
    else {
      // 저장돼있지 않으면 처음이므로 저장;
      Log.w("APP# SplashActivity | verifyUuid", "|" + "저장돼있지 않으면 처음이므로 저장");
      setDeviceUuid(currentUuid);
    }
  }

  private void setDeviceUuid(String uuid) {
    Log.w("APP# SplashActivity | setDeviceUuid", "| " + uuid);
    SuperModel.getInstance().setDeviceUuid(uuid);
  }
}
