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

import android.app.ActionBar;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import kr.co.zumo.app.R;
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.view.Event;
import kr.co.zumo.app.lifeplus.view.ISignUpView;

import kr.co.zumo.app.lifeplus.view.fragment.signup.SignUpPhoneAuthorizationFragment;
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.IDialogResultListener;
import kr.co.zumo.app.lifeplus.view.presenter.SignUpPresenter;

/**
 * ;
 * SignUpActivity
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 5.]   [최초 작성]
 * @since 2018. 9. 5.
 */
public class SignUpActivity extends ActivityBase<SignUpPresenter> implements ISignUpView {

  @Override
  void onAfterCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_sign_up);

//    FragmentManager manager = getSupportFragmentManager();
//    FragmentTransaction fragmentTransaction = manager.beginTransaction();
//    SignUpStartFragment fragment = new SignUpStartFragment();
//    fragmentTransaction.add(R.id.sign_up_container, fragment);
//    fragmentTransaction.commit();

//    adapter = new SignUpAdapter(presenter);
//
//    recyclerView = findViewById(R.id.sign_up_list);
//    recyclerView.setHasFixedSize(true);
//    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(App.getInstance().getContext());
//    recyclerView.setLayoutManager(layoutManager);
//    recyclerView.setAdapter(adapter);


//    presenter.start();

    DialogBuilder.create(DialogID.MOBILE_AUTHORIZATION)
      .listener(new IDialogResultListener() {
        @Override
        public void onDialogResult(DialogBase dialog, Event event) {
          Log.i("APP# SignUpActivity | ", "|" + "resultString: " + event.getString());
          int eventId = event.getEventId();
          if (Event.SUCCESS == eventId) {
            dialog.dismiss();
            LifeplusAPIBean bean = event.fromJson(LifeplusAPIBean.class);

            Log.w("APP# SignUpActivity | onDialogResult", "| " + bean.toPrettyJson());

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

          }
          else if (Event.FAIL == eventId) {
            // todo 모바일 인증 실패
          }
        }

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

  @Override
  protected void onDestroy() {
    super.onDestroy();
    //presenter.dispose();
  }


  @Override
  void defineActionBar() {
    ActionBarManager actionBarManager = ActionBarManager.getInstance();
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.sign_up_action_bar, null);

    actionBarManager.setCustomView(view);
    actionBarManager.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBarManager.setElevation(0);

    TextView actionBarTitle = view.findViewById(R.id.action_bar_title);
    actionBarTitle.setText("회원가입");


  }

  @Override
  SignUpPresenter definePresenter() {
    return new SignUpPresenter(new SignUpModel(), this);
  }

  /**
   * recyclerView 에 아이템이 변경되었음을 알려준다..
   */
  @Override
  public void notifyItemChanged() {
    //adapter.notifyItemChanged(recyclerView, presenter.getSignUpList());
  }

  public void onFragmentChanged(int index){
    if(index == 0){
      SignUpPhoneAuthorizationFragment fragment = new SignUpPhoneAuthorizationFragment();
      getSupportFragmentManager().beginTransaction().replace(R.id.sign_up_container,fragment).commit();
    }
  }
}

