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

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.model.SignUpModel;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.ISignUpView;
import kr.co.zumo.app.lifeplus.view.custom.pin.CustomPinRegisterPresenter;
import kr.co.zumo.app.lifeplus.view.custom.pin.CustomPinView;
import kr.co.zumo.app.lifeplus.view.custom.pin.IPinContract;
import kr.co.zumo.app.lifeplus.view.fragment.FragmentBase;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpPinPresenter;

/**
 * SignUpPinFragment
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 28.]   [최초 작성]
 * @since 2018. 9. 28.
 */
public class SignUpPinFragment extends FragmentBase<SignUpPinPresenter> implements ISignUpView {

  private CustomPinView customPinView;
  private IPinContract.Presenter pinPresenter;

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.view_container, container, false);
  }

  @Override
  protected void onAfterActivityCreated(Bundle savedInstanceState) {

    /**
     * 핀 확인/등록/초기화(재등록)
     *
     * - 같은 뷰(CustomPinView)를 이용한다.
     * - presenter 를 달리하여 각 단계를 컨트롤한다.
     * - 결과는 IPinContract.Listener 로 전달한다.
     *
     */
    customPinView = new CustomPinView(getContext());
    pinPresenter = new CustomPinRegisterPresenter(customPinView, new IPinContract.Listener() {
      @Override
      public void onPinResult(String result) {
        presenter.onEvent(new Event.Builder(Event.SUCCESS).string(result).build());
      }

      @Override
      public void onClickPinReset() {
      }

      @Override
      public void onPinFail() {
        pinPresenter.onPinStart();
      }
    });

    View parent = getView();
    ViewGroup layout = parent.findViewById(R.id.container);
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layout.addView(customPinView, params);
  }

  @Override
  protected SignUpPinPresenter definePresenter() {
    return new SignUpPinPresenter(getModel(SignUpModel.class), this);
  }
}
