package kr.co.zumo.app.lifeplus.view.fragment.signup;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.view.presenter.SignUpPresenter;
import kr.co.zumo.app.lifeplus.view.presenter.signup.SignUpItem;

/**
 * 회원 가입 리스트 뷰 화면의 관리하는 어댑터 클래스
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-11]   [최초 작성]
 * @since 2018-09-11
 */
public class SignUpAdapter extends RecyclerView.Adapter<BaseViewHolder> {

  private SignUpPresenter presenter;

  public SignUpAdapter(SignUpPresenter presenter) {
    this.presenter = presenter;
  }

  int prevLen = 0;

  private View getWrappedView(ViewGroup parent, View view) {

    LinearLayout layout = new LinearLayout(parent.getContext());
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(param);
    layout.setOrientation(LinearLayout.HORIZONTAL);

    layout.addView(view);

    return layout;
  }

  /**
   * item 변경을 알려 뷰를 업데이트 한다.
   *
   * @param recyclerView
   * @param items
   */
  public void notifyItemChanged(RecyclerView recyclerView, List<SignUpItem> items) {
    int len = items.size();
    Log.i("APP# SignUpFragment | setSignUpItems", "|" + "prevLen: " + prevLen + ", items.size(): " + len);

    if (len > prevLen) {
      notifyItemInserted(len - 1);
    }
    else {
      notifyItemRemoved(prevLen - 1);
    }

    recyclerView.smoothScrollToPosition(len);
    prevLen = len;
  }

  @NonNull
  @Override
  public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    BaseViewHolder holder = null;
    View view;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
//    Log.e("APP# SignUpAdapter | onCreateViewHolder", "| index: " + index + " viewType: " + viewType + ", alignType: " + alignType);
    switch (viewType) {
      case SignUpItem.SIGN_UP_TEXT:
        view = inflater.inflate(R.layout.sign_up_text_view, parent, false);
        view = getWrappedView(parent, view);
        holder = new TextViewHolder(view);
        break;
      case SignUpItem.SIGN_UP_BENEFIT:
        view = inflater.inflate(R.layout.sign_up_benefit_list, parent, false);
        view = getWrappedView(parent, view);
        holder = new BenefitHolder(view);
        break;
      case SignUpItem.SIGN_UP_BUTTON:
        view = inflater.inflate(R.layout.sign_up_button, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpButtonHolder(view);
        break;
      case SignUpItem.SIGN_UP_TWO_BUTTON:
        view = inflater.inflate(R.layout.sign_up_text_view, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpTwoButtonHolder(view);
        break;
      case SignUpItem.SIGN_UP_COIN:
        view = inflater.inflate(R.layout.sign_up_coin, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpCoinHolder(view);
        break;
      case SignUpItem.SIGN_UP_NOTICE:
        view = inflater.inflate(R.layout.sign_up_text_view, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpNoticeHolder(view);
        break;
      case SignUpItem.SIGN_UP_TERMS:
        view = inflater.inflate(R.layout.sign_up_text_view, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpTermsHolder(view);
        break;
      case SignUpItem.SIGN_UP_TEXT_WITH_TITLE:
        view = inflater.inflate(R.layout.sign_up_identify, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpTextWithTitleHolder(view);
        break;
      case SignUpItem.SIGN_UP_WAITING:
        view = inflater.inflate(R.layout.sign_up_text_view, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpWaitingHolder(view);
        break;
      case SignUpItem.SIGN_UP_DIVIDER:
        view = inflater.inflate(R.layout.sign_up_divider, parent, false);
        view = getWrappedView(parent, view);
        holder = new SignUpDividerHolder(view);
        break;
//      case SignUpItem.SIGN_UP_AGREE:
//        view = inflater.inflate(R.layout.sign_up_agree, parent, false);
//        view = getWrappedView(parent, view);
//        holder = new SignUpAgreeHolder(view, presenter.getAgreeItems());
//        break;
      default:
        break;
    }
    return holder;
  }


  @Override
  public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
    SignUpItem item = presenter.getItem(position);
    holder.draw(item);
    holder.align(item);
    holder.notifyCreation(item);
//    Log.w("APP# SignUpAdapter | onBindViewHolder", "|" + new Gson().toJson(item));
  }

  @Override
  public int getItemCount() {
    return presenter.getSignUpList().size();
  }

  @Override
  public int getItemViewType(int position) {
    SignUpItem item = presenter.getItem(position);
    return item.getItemType();
  }
}
