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

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import kr.co.zumo.app.lifeplus.bean.SignUpAgreeItemBean;
import kr.co.zumo.app.lifeplus.bean.SignUpBean;
import kr.co.zumo.app.lifeplus.model.SignUpModel;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.util.StringUtil;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.ISignUpView;
import kr.co.zumo.app.lifeplus.view.command.ActivityChangeCommand;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpItem;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpStep;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpStepBuilder;

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

  List<SignUpItem> signUpList;
  SignUpStep step;
  ArrayList<SignUpAgreeItemBean> items;

  // 회원 가입에 필요한 데이터
  SignUpBean signUpBean;

  public SignUpPresenter(SignUpModel model, ISignUpView view) {
    super(model, view);

    signUpList = new ArrayList<>();
    items = new ArrayList<>();
    signUpBean = new SignUpBean();

    prepareFlow();
  }

  private void prepareFlow() {
    step = SignUpStepBuilder.create(this)
      .hello()
      .benefit()
      .confirm()
      .pin()
      .agree()
      .complete()
      .build();
  }

  @Override
  public void start() {
    step.start();
  }

  @Override
  protected void onDispose() {
    Log.i("APP# SignUpPresenter | onDispose", "|" + "-------------------------> ");
    if (null != step) {
      step.dispose();
      step = 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()) {
      case Event.JOINED:
        onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_MAIN, ScreenID.DIRECTION_NEXT));
        break;
      default:
        break;
    }
  }


  /**
   * 화면 구성을 이루는 데이터 객체 배열 반환
   * - recyclerView 의 데이터로 사용
   *
   * @return ObservableList<SignUpItem>
   */
  public List<SignUpItem> getSignUpList() {
    return signUpList;
  }

  /**
   * 데이터를 추가하여 view 표시
   *
   * @param item SignUpItem
   */
  public void addItem(SignUpItem item) {
    signUpList.add(item);
    Log.w("APP# SignUpFragmentPresenter | addItem", "| signUpList.size(): " + signUpList.size());
    view.notifyItemChanged();
  }

  /**
   * 마지막 데이터 삭제
   */
  public void removeLastItem() {
    signUpList.remove(signUpList.size() - 1);
    Log.w("APP# SignUpFragmentPresenter | removeLastItem", "| signUpList.size(): " + signUpList.size());
    view.notifyItemChanged();
  }

  /**
   * 인덱스에 해당하는 SignUpItem 객체 반환
   *
   * @param index index
   */
  public SignUpItem getItem(int index) {
    return signUpList.get(index);
  }

  /**
   * 간편입력 코드 (pin) 저장
   *
   * @param pin "0192"
   */
  public void setPin(String pin) {
    try {
      signUpBean.setEncryptedPin(StringUtil.convertToSHA256(StringUtil.convertToMD5(pin)));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 약관 동의 데이터 (SignUpAgreeItemBean) 하나 추가
   * - api 를 통해서 전달받은 약관 양식을 bean 으로 파싱하여 추가한다.
   *
   * @param item SignUpAgreeItemBean
   */
  public void addAgreeItem(SignUpAgreeItemBean item) {
    this.items.add(item);
  }

  /**
   * 약관 동의 데이터 bean 반환
   *
   * @return
   */
  public ArrayList<SignUpAgreeItemBean> getAgreeItems() {
    return items;
  }

  /**
   * 약관 동의 데이터 저장
   * - api 를 통해서 전달받은 약관 양식을 bean 으로 파싱하여 저장한다.
   *
   * @param items ArrayList<SignUpAgreeItemBean>
   */
  public void setAgreeItems(ArrayList<SignUpAgreeItemBean> items) {
    this.items = items;
  }
}
