/*
 * 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.TextBean;
import kr.co.zumo.app.lifeplus.util.ResourceUtil;
import kr.co.zumo.app.lifeplus.util.StringUtil;
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.IAttribute;
import kr.co.zumo.app.lifeplus.view.dialog.IDialogResultListener;
import kr.co.zumo.app.lifeplus.view.dialog.PinDialog;
import kr.co.zumo.app.lifeplus.view.fragment.signup.ISignUpHolderEventListener;
import kr.co.zumo.app.lifeplus.view.presenter.SignUpPresenter;

/**
 * SignUpStepPin
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 14.]   [최초 작성]
 * @since 2018. 9. 14.
 */
public class SignUpStepPin extends SignUpStep {
  public SignUpStepPin(SignUpPresenter model) {
    super(model);
  }

  private String pin;

  ISignUpHolderEventListener buttonEventHandler = (holder, eventId, type, param) -> {
    if (SignUpItem.BUTTON_ID_PIN == type) {

      // 핀 번호 입력 다이얼 로그 호출
      Log.i("APP# SignUpStepPin | onSignUpHolderEvent", "| eventId: " + eventId + ", type: " + type);
      DialogBuilder.create(DialogID.INPUT_PIN)
        .attribute((IAttribute<PinDialog>) dialog -> {
          dialog.updateGuideText(R.string.sign_up_register_pin);
          dialog.updateTitleText(R.string.sign_up_register_pin_title);
        })
        .listener(new IDialogResultListener() {
          @Override
          public void onDialogResult(DialogBase dialog, Event event) {
            int eventId = event.getEventId();
            String result = event.getString();
            Log.i("APP# SignUpStepPin | ", "|" + "resultCode: " + eventId + "resultString: " + result);
            if (Event.SUCCESS == eventId) {
              if (StringUtil.isFull(pin)) {
                // 두 번째 등록

                dialog.setDisabled();
                // 일치하는지 확인
                if (pin.length() == 4 && pin.equals(result)) {
                  // 일치 하면 모델에 저장
                  presenter.setPin(pin);
                  timeout(200, () -> {
                    // 일단 창 닫기
                    dialog.dismiss();
                    // 버튼 제거
                    removeLastItem();
                    addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_END, new TextBean(ResourceUtil.getString(R.string.sign_up_pin_confirm)).toJson()));
                    timeout(1000, () -> {
                      addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_START, new TextBean(ResourceUtil.getString(R.string.sign_up_pin_confirm_completed)).toJson()));
                      timeout(1000, () -> callNextStep());
                    });
                  });

                }
                else {
                  // 일치 하지 않으면 저장소 초기화, 채팅 메세지 표시하고 입력창 다시 오픈
                  pin = null;
                  timeout(200, () -> {
                    // 일단 창 닫기
                    dialog.dismiss();
                    // 버튼 제거
                    removeLastItem();
                    addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_END, new TextBean(ResourceUtil.getString(R.string.sign_up_input_pin_fail)).toJson()));
                    timeout(1000, () -> {
                      addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_START, new TextBean(ResourceUtil.getString(R.string.sign_up_need_pin)).toJson()));
                      timeout(1000, () -> addRegisterButton());
                    });
                  });

                }
              }
              else {
                pin = result;
                dialog.setDisabled();
                timeout(200, () -> {
                  PinDialog pinDialog = ((PinDialog) dialog);
                  pinDialog.updateGuideText(R.string.sign_up_register_pin_again);
                  pinDialog.updateTitleText(R.string.sign_up_register_pin_title);
                  pinDialog.restart();
                });
              }
            }
          }

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

  @Override
  void __start() {
    presenter.addItem(new SignUpItem(SignUpItem.SIGN_UP_DIVIDER, new TextBean(ResourceUtil.getString(R.string.sign_up_input_pin)).toJson()));
    presenter.addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_START, new TextBean(ResourceUtil.getString(R.string.sign_up_need_pin)).toJson()));
    addRegisterButton();
  }

  private void addRegisterButton() {
    presenter.addItem(new SignUpItem(SignUpItem.SIGN_UP_BUTTON, SignUpItem.ALIGN_CENTER, new SignUpButtonBean(ResourceUtil.getString(R.string.sign_up_input_pin), SignUpItem.BUTTON_ID_PIN).toJson(), buttonEventHandler));
  }

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

  @Override
  void __dispose() {

  }
}
