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

import android.util.Log;

import com.google.gson.Gson;

import java.util.List;

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

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

  BaseViewHolder agreeHolder;
  BaseViewHolder buttonHolder;

  private ISignUpHolderEventListener buttonEventListener = (holder, eventId, type, param) -> {
    if (Event.HOLDER_CREATED == eventId) {
      buttonHolder = holder;
      setButtonUsage();
    }
    else if (Event.CLICK == eventId) {
      removeLastItem();

      if (isValidated) {
        // 필수 동의 했음
        Log.i("APP# SignUpStepAgree | ", "|" + new Gson().toJson(presenter.getAgreeItems()));

        agreeHolder.setDisabled();
        addItem(new SignUpItem(SignUpItem.SIGN_UP_TEXT, SignUpItem.ALIGN_END, new TextBean(ResourceUtil.getString(R.string.sign_up_agree_completed)).toJson()));
        timeout(1000, () -> callNextStep());
      }
      else {
        // 동의 모자름
        // -> 버튼 다시 생성
        // todo 실패 시나리오 적용 필요;
        addCompleteButton();
      }
    }
  };

  private void setButtonUsage() {

    if (isValidated) {
      buttonHolder.setEnabled();
    }
    else {
      buttonHolder.setDisabled();
    }
  }


  @Override
  void __start() {
    /**
     * todo 서버 연동 필요
     *
     * - 서버에서 약관 양식 가져옴;
     * - bean 으로 파싱
     * - presenter 에 저장 해둔다. holder 가 재활용 될 경우 데이터를 공유해야하기 때문에 presenter 의 데이터를 직접 사용한다.
     *
     */

    presenter.addAgreeItem(new SignUpAgreeItemBean(ResourceUtil.getString(R.string.service_agree0), true, "https://google.com"));
    presenter.addAgreeItem(new SignUpAgreeItemBean(ResourceUtil.getString(R.string.service_agree1), true, "https://google.co.kr"));
    presenter.addAgreeItem(new SignUpAgreeItemBean(ResourceUtil.getString(R.string.service_agree2), false, "https://google.co.jp"));
    presenter.addAgreeItem(new SignUpAgreeItemBean(ResourceUtil.getString(R.string.service_agree3), false, "https://google.co.uk"));
    presenter.addAgreeItem(new SignUpAgreeItemBean(ResourceUtil.getString(R.string.service_agree4), false, "https://google.co.de"));
    presenter.addAgreeItem(new SignUpAgreeItemBean(ResourceUtil.getString(R.string.service_agree5), false, "https://daum.net"));

    addItem(new SignUpItem(SignUpItem.SIGN_UP_DIVIDER, new TextBean(ResourceUtil.getString(R.string.sign_up_agree)).toJson()));
    addItem(new SignUpItem(SignUpItem.SIGN_UP_AGREE, SignUpItem.ALIGN_START, (holder, eventId, type, param) -> {
      if (Event.HOLDER_CREATED == eventId) {
        agreeHolder = holder;
      }
      else if (Event.HOLDER_AGREE_CHANGED == eventId) {
        Logg.i("APP# SignUpStepAgree | onSignUpHolderEvent", "|" + type);

        __validate();

        setButtonUsage();
      }
      else if (Event.HOLDER_DETAIL == eventId) {
        SignUpAgreeItemBean bean = new Gson().fromJson(param, SignUpAgreeItemBean.class);

        Log.i("APP# SignUpStepAgree | __start", "|" + bean.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>) dialog -> dialog.setUrl(bean.getDetailUrl()))
          .show();

      }
    }));

    addCompleteButton();
  }

  private void addCompleteButton() {
    addItem(new SignUpItem(SignUpItem.SIGN_UP_BUTTON, SignUpItem.ALIGN_CENTER, new SignUpButtonBean(ResourceUtil.getString(R.string.sign_up_agree), SignUpItem.BUTTON_AGREE_COMPLETE).toJson(), buttonEventListener));
  }

  @Override
  void __validate() {
    // SIGN_UP_AGREE 에서 이벤트로 변경 됨
    List<SignUpAgreeItemBean> agreeBean = presenter.getAgreeItems();

    isValidated = true;
    for (SignUpAgreeItemBean bean : agreeBean) {
      if (bean.isRequired()) {
        if (false == bean.isChecked()) {
          isValidated = false;
          break;
        }
      }
    }
  }

  @Override
  void __dispose() {
    agreeHolder = null;
    buttonHolder = null;
  }
}
