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.auth.IMobileAuthContract;
import kr.co.zumo.app.lifeplus.view.custom.auth.MobileAuthPresenter;
import kr.co.zumo.app.lifeplus.view.custom.auth.MobileAuthView;
import kr.co.zumo.app.lifeplus.view.fragment.FragmentBase;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpMobileAuthPresenter;

/**
 * SignUpMobileAuthFragment
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-27]   [최초 작성]
 * @since 2018-09-27
 */
public class SignUpMobileAuthFragment extends FragmentBase<SignUpMobileAuthPresenter> implements ISignUpView {

  private MobileAuthView authView;
  private IMobileAuthContract.Presenter authPresenter;

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @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) {

    authView = new MobileAuthView(getContext());
    authPresenter = new MobileAuthPresenter(authView, event -> {
      if (Event.SUCCESS == event.getEventId()) {
        presenter.onEvent(event);
      }
    });

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

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


  @Override
  public void onDestroyView() {
    super.onDestroyView();

    if (null != authView) {
      authView.dispose();
    }

    if (null != authPresenter) {
      authPresenter.dispose();
    }
  }
}
