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

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.application.App;
import kr.co.zumo.app.lifeplus.bean.MobileAuthorizationBean;
import kr.co.zumo.app.lifeplus.bean.api.LifeplusAPIBean;
import kr.co.zumo.app.lifeplus.manager.ActionBarManager;
import kr.co.zumo.app.lifeplus.model.SignUpModel;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.util.AppUtil;
import kr.co.zumo.app.lifeplus.util.Formatter;
import kr.co.zumo.app.lifeplus.util.StringUtil;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.ISignUpView;
import kr.co.zumo.app.lifeplus.view.command.FragmentChangeCommand;

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

  public SignUpMobileAuthorizationPresenter(SignUpModel model, ISignUpView view) {
    super(model, view);
  }

  @Override
  protected void startInternal() {
    ActionBarManager.getInstance().setProgress(25); // 90/360
  }

  @Override
  protected void stopInternal() {

  }

  @Override
  protected void resumeInternal() {

  }

  @Override
  protected void pauseInternal() {

  }

  @Override
  protected void destroyInternal() {

  }

  @Override
  public void onResult(Event event) throws Throwable {

  }

  @Override
  public boolean onBackPressed() {
    onCommand(new FragmentChangeCommand(ScreenID.SIGN_UP_START, ScreenID.DIRECTION_BACK));
    return true;
  }

  @Override
  protected void onEventInternal(Event event) {
    switch (event.getEventId()) {
      case Event.CONFIRM:
        onCommand(new FragmentChangeCommand(ScreenID.SIGN_UP_PIN, ScreenID.DIRECTION_NEXT));
        break;
      case Event.SUCCESS:
        Log.i("APP# SignUpMobileAuthorizationPresenter | onEventInternal", "|" + "resultString: " + event.getJson());
        onAuthResult(event);
        break;
      case Event.FAIL:
        // todo 모바일 인증 실패
        break;
      case Event.CANCEL:
        // fixme for test
        if (AppUtil.isDebug()) {
          onEvent(new Event.Builder(Event.CONFIRM).build());
        }
        break;
      default:
        break;
    }
  }

  public void onAuthResult(Event event) {
    MobileAuthorizationBean mobileAuthorizationBean = event.fromJson(MobileAuthorizationBean.class);
    LifeplusAPIBean resultBean = new Gson().fromJson(event.getString(), LifeplusAPIBean.class);

    Log.w("APP# SignUpMobileAuthorizationPresenter | onEventInternal", "| 인증 결과: " + resultBean.toPrettyJson());
    Log.w("APP# SignUpMobileAuthorizationPresenter | onEventInternal", "| 유저 데이터: " + mobileAuthorizationBean.toPrettyJson());

    /**
     * // todo 아래 코드는 팝업 외에서 처리해야 한다.
     * 2. 성공 시 PROCESS 진입
     1) 만 14세 이상 : 약관동의(JO2001)화면 링크
     2) 14세 미만 : 14세 미만 가입불가 안내 (JO5002) 화면 링크
     3) 회원 Y: 기존회원 간편암호 확인 안내 (JO5002) 화면 링크 
     4) 회원탈퇴일 기준 30일 이내 Y
     - 탈퇴 회원 (30일 이내) 가입불가 안내 (JO5003) 화면 링크
     */

    if (false) {
      //todo 14세 미만
    }
    else if (false) {
      // todo 기존 회원
    }
    else if (false) {
      // todo 30일 이내 재가입
    }
    else {
      Context context = App.getInstance().getContext();
      Toast toast = new Toast(context);
      toast.setDuration(Toast.LENGTH_LONG);
      toast.setGravity(Gravity.FILL, 0, 0);
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.sign_up_authorization_result_dialog, null);
      ((TextView) view.findViewById(R.id.text_view_authorization_result_name)).setText(mobileAuthorizationBean.getName());
      ((TextView) view.findViewById(R.id.text_view_authorization_result_gender)).setText(mobileAuthorizationBean.getGender());
      ((TextView) view.findViewById(R.id.text_view_authorization_result_birth_date)).setText(
        Formatter.format(mobileAuthorizationBean.getBirthDate(), MobileAuthorizationBean.BIRTH_DATE_FORMAT, "yyyy.mm.dd")
      );
      String phoneNumber = mobileAuthorizationBean.getFirstPhoneNumber() + mobileAuthorizationBean.getPhoneNumber();
      Log.i("APP# SignUpMobileAuthorizationPresenter | onAuthResult", "| b: " + phoneNumber);
      Log.i("APP# SignUpMobileAuthorizationPresenter | onAuthResult", "| a: " + StringUtil.formatPhoneNumber(phoneNumber));
      ((TextView) view.findViewById(R.id.text_view_authorization_result_phone_number)).setText(
        StringUtil.formatPhoneNumber(phoneNumber)
      );
      toast.setView(view);
      toast.show();

      // 신규 가입
      model.setMobileAuthorizationBean(mobileAuthorizationBean);
      model.checkMobileAuthorized();

      onEvent(new Event.Builder(Event.CONFIRM).build());
    }
  }

}
