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

import java.util.List;

import kr.co.zumo.app.lifeplus.bean.SignUpAgreeItemBean;
import kr.co.zumo.app.lifeplus.bean.api.PolicyBean;
import kr.co.zumo.app.lifeplus.manager.ActionBarManager;
import kr.co.zumo.app.lifeplus.model.SignUpModel;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.command.FragmentChangeCommand;
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;
import kr.co.zumo.app.lifeplus.view.dialog.WebDialog;
import kr.co.zumo.app.lifeplus.view.presenter.Presenter;

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

  protected boolean isValidated = false;
  protected boolean isLoaded = false;
  protected boolean isViewReady = false;

  public SignUpAgreePresenter(SignUpModel model, ISignUpAgreeView view) {
    super(model, view);

    this.model.requestPolicy();
  }

  @Override
  protected void startInternal() {
    ActionBarManager.getInstance().setProgress(75); // 270/360
  }

  @Override
  protected void stopInternal() {

  }

  @Override
  protected void resumeInternal() {

  }

  @Override
  protected void pauseInternal() {

  }

  @Override
  protected void destroyInternal() {
    model.stopPolicyRequest();
  }

  @Override
  public void onResult(Event event) throws Throwable {

    switch (event.getEventId()) {
      case Event.RESULT:
        if (event.getInteger() == SignUpModel.EVENT_POLICY_REQUEST) {
          isLoaded = true;
          renderAfterReady();
        }
        else if (event.getInteger() == SignUpModel.EVENT_POLICY_CONFIRM) {
          // 약관 동의 전송 했음;
          signUp();
        }
        else if (event.getInteger() == SignUpModel.EVENT_JOINED) {
          onCommand(new FragmentChangeCommand(ScreenID.SIGN_UP_COMPLETED, ScreenID.DIRECTION_NEXT));
        }
        break;
      case Event.ERROR:
        // todo error
        if (event.getInteger() == SignUpModel.EVENT_POLICY_REQUEST) {
        }
        else if (event.getInteger() == SignUpModel.EVENT_POLICY_CONFIRM) {
        }
        else if (event.getInteger() == SignUpModel.EVENT_JOINED) {
        }
        break;
      default:
        break;
    }
  }

  private void renderAfterReady() {
    if (isLoaded && isViewReady) {
      view.init(getAgreeItems());
    }
  }

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

  @Override
  protected void onEventInternal(Event event) {
    switch (event.getEventId()) {
      case Event.SUCCESS:
        // 동의 버튼 눌렀음
        // 약관 동의 전송
        sendPolicyConfirm();
        break;
      case Event.CHANGED:
        onAgreeChanged();
        break;
      case Event.INIT:
        isViewReady = true;
        renderAfterReady();
        break;
      case Event.DETAIL:
        onClickDetail(event);
        break;
      default:
        break;
    }
  }

  private void sendPolicyConfirm() {
    model.sendPolicy();
  }

  private void onClickDetail(Event event) {
    String url = event.fromJson(SignUpAgreeItemBean.class).getDetailUrl();
    DialogBuilder.create(DialogID.WEB)
      .listener(new IDialogResultListener() {
        @Override
        public void onDialogResult(DialogBase dialog, Event event) {

        }

        @Override
        public void onDialogCanceled(DialogBase dialog) {

        }
      })
      .attribute((IAttribute<WebDialog>) d -> d.setUrl(url))
      .show();
  }

  private void onAgreeChanged() {

    validate();
    setButtonUsage();
  }

  /**
   * 약관 동의 데이터 bean 반환
   *
   * @return
   */
  private List<PolicyBean> getAgreeItems() {
    return model.getAgreeItems();
  }

  private void validate() {
    // SIGN_UP_AGREE 에서 이벤트로 변경 됨
    isValidated = model.isValidatedPolicy();
  }

  private void setButtonUsage() {
    view.setAgreeButtonEnabled(isValidated);
  }

  private void signUp() {
    /**
     * 회원 가입 절차 시작
     */

    model.signUp();
  }
}
