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

import android.util.Log;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.bean.SignUpButtonBean;
import kr.co.zumo.app.lifeplus.bean.SignUpTextWithTitleBean;
import kr.co.zumo.app.lifeplus.bean.TextBean;
import kr.co.zumo.app.lifeplus.util.ResourceUtil;
import kr.co.zumo.app.lifeplus.view.Event;
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.IDialogResultListener;
import kr.co.zumo.app.lifeplus.view.fragment.signup.BaseViewHolder;
import kr.co.zumo.app.lifeplus.view.fragment.signup.ISignUpHolderEventListener;
import kr.co.zumo.app.lifeplus.view.presenter.SignUpPresenter;

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

  public SignUpStepAuthorization(SignUpPresenter model) {
    super(model);
  }

  @Override
  void __start() {

    addItem(new SignUpItem(SignUpItem.SIGN_UP_DIVIDER, new TextBean(ResourceUtil.getString(R.string.sign_up_identify)).toJson()));
    addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_START, new TextBean(ResourceUtil.getString(R.string.sign_up_need_id)).toJson()));
    addItem(new SignUpItem(SignUpItem.SIGN_UP_BUTTON, SignUpItem.ALIGN_CENTER, new SignUpButtonBean(ResourceUtil.getString(R.string.sign_up_mobile_identify), SignUpItem.BUTTON_MOBILE_AUTHORIZATION).toJson(), buttonEventListener));

  }

  private BaseViewHolder buttonHolder;
  private ISignUpHolderEventListener buttonEventListener = (holder, eventId, type, param) -> {
    Log.i("APP# SignUpStepAuthorization | ", "|" + "holder: " + holder + ", eventId: " + eventId + ", type: " + type);
    if (Event.HOLDER_CREATED == eventId) {
      buttonHolder = holder;
    }
    else if (Event.CLICK == eventId) {
      if (SignUpItem.BUTTON_MOBILE_AUTHORIZATION == type) {
        DialogBuilder.create(DialogID.MOBILE_AUTHORIZATION)
          .listener(new IDialogResultListener() {
            @Override
            public void onDialogResult(DialogBase dialog, Event event) {
              Log.i("APP# SignUpStepAuthorization | ", "|" + "resultString: " + event.getString());
              int eventId = event.getEventId();
              if (Event.SUCCESS == eventId) {
                dialog.dismiss();

                removeLastItem();
                addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_END, new TextBean(ResourceUtil.getString(R.string.sign_up_mobile_confirm)).toJson()));

                timeout(1000, () -> {
                  addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT_WITH_TITLE, SignUpItem.ALIGN_START, new SignUpTextWithTitleBean(ResourceUtil.getString(R.string.sign_up_id_confirm_completed), "김한화, 남\n1982.02.20\n010-1234-5678").toJson()));

                  timeout(1000, SignUpStepAuthorization.this::callNextStep);
                });
              }
              else if (Event.FAIL == eventId) {
                // todo 모바일 인증 실패
              }
            }

            @Override
            public void onDialogCanceled(DialogBase dialog) {
              buttonHolder.setEnabled();
            }
          })
          .show();

      }
    }
  };

  @Override
  void __validate() {
    isValidated = true;
  }

  @Override
  void __dispose() {
  }
}
