/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.view.invitation;

import com.ntels.onecable.R;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.c_interface.ListInterface;
import com.ntels.onecable.common.util.Util;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * <pre>
 * 1. ClassName :   InvitationListAdapter
 * 2. FileName  :   InvitationListAdapter.java
 * 3. Package   :   com.ntels.onecable.view.invitation
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class InvitationListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  public static final int TYPE_HEADER = 0;
  public static final int TYPE_ITEM = 1;
  public static final int TYPE_FOOTER = 2;

  private ListInterface adapterCallback;

  private Util util = new Util();
  private ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<>();
  private Context context;
  private int listLayoutHeader;
  private int listLayoutItem;
  private int listLayoutFooter;
  private Boolean isListCheck = false;
  private Boolean isHeaderCheck = false;
  private Boolean isFooterCheck = false;

  /**
   * {@link #isListCheck} setter.
   *
   * @param isCheck true or false
   */
  public void setListCheck(boolean isCheck) {

    isListCheck = isCheck;
  }

  /**
   * {@link #isListCheck} getter.
   *
   * @return true or false
   */
  public boolean getListCheck() {

    return isListCheck;
  }

  /**
   * {@link #isHeaderCheck} setter.
   *
   * @param isCheck true or false
   */
  public void setHeaderCheck(boolean isCheck) {

    isHeaderCheck = isCheck;
  }

  /**
   * {@link #isHeaderCheck} getter.
   *
   * @return true or false
   */
  public boolean getHeaderCheck() {

    return isHeaderCheck;
  }

  /**
   * {@link #isFooterCheck} setter.
   *
   * @param isCheck true or false
   */
  public void setFooterCheck(boolean isCheck) {

    isFooterCheck = isCheck;
  }

  /**
   * {@link #isFooterCheck} getter.
   *
   * @return true or false
   */
  public boolean getFooterCheck() {

    return isFooterCheck;
  }

  @Override
  public int getItemCount() {

    return alTotalList.size();
  }

  /**
   * 생성자.
   *
   * @param context 컨텍스트
   * @param headerLayoutItem Header layout resource id
   * @param listLayoutItem Item layout resource id
   * @param footerLayoutItem Footer layout resource id
   * @param alTmp RecyclerView에 보여줄 data
   * @param adapterCallback Callback listener
   */
  public InvitationListAdapter(Context context, int headerLayoutItem, int listLayoutItem,
                               int footerLayoutItem, ArrayList<HashMap<String, Object>> alTmp,
                               ListInterface adapterCallback) {

    this.context = context;
    alTotalList = alTmp;
    listLayoutHeader = headerLayoutItem;
    this.listLayoutItem = listLayoutItem;
    listLayoutFooter = footerLayoutItem;
    try {
      this.adapterCallback = adapterCallback;
    } catch (ClassCastException ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == TYPE_ITEM) {
      View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutItem, parent, false);
      return new ItemViewHolder(view);
    } else if (viewType == TYPE_HEADER) {
      View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutHeader, parent, false);
      return new HeaderViewHolder(view);
    } else if (viewType == TYPE_FOOTER) {
      View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutFooter, parent, false);
      return new FooterViewHolder(view);
    }

    throw new RuntimeException("there is no type that matches the type " + viewType
        + " + make sure your using types correctly");
  }

  /**
   * <pre>
   * 1. ClassName :   HeaderViewHolder
   * 2. FileName  :   InvitationListAdapter.java
   * 3. Package   :   com.ntels.onecable.view.invitation
   * 4. Date      :   2016-12-01
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   * </pre>
   */
  public static class HeaderViewHolder extends RecyclerView.ViewHolder {

    /**
     * 생성자.
     *
     * @param itemView Item layout view
     */
    public HeaderViewHolder(View itemView) {

      super(itemView);

    }
  }

  /**
   * <pre>
   * 1. ClassName :   ItemViewHolder
   * 2. FileName  :   InvitationListAdapter.java
   * 3. Package   :   com.ntels.onecable.view.invitation
   * 4. Date      :   2016-12-01
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   * </pre>
   */
  public static class ItemViewHolder extends RecyclerView.ViewHolder {

    public LinearLayout llRowRoot;
    public LinearLayout llRowBody;
    public LinearLayout llRowBodyBody;
    public LinearLayout llRowBodyCmd;
    public ImageView ivBodyImage;
    public TextView tvBodyTitle;
    public TextView tvBodyBody;
    public TextView tvCmdOk;
    public TextView tvCmdCancel;

    /**
     * 생성자.
     *
     * @param itemView Item layout view
     */
    public ItemViewHolder(View itemView) {

      super(itemView);
      try {
        llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
        llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
        llRowBodyBody = (LinearLayout) itemView.findViewById(R.id.llRowBody_Body);
        llRowBodyCmd = (LinearLayout) itemView.findViewById(R.id.llRowBody_Cmd);
        ivBodyImage = (ImageView) itemView.findViewById(R.id.ivBodyImage);
        tvBodyTitle = (TextView) itemView.findViewById(R.id.tvBodyTitle);
        tvBodyBody = (TextView) itemView.findViewById(R.id.tvBodyBody);
        tvCmdOk = (TextView) itemView.findViewById(R.id.tvCmdOk);
        tvCmdCancel = (TextView) itemView.findViewById(R.id.tvCmdCancel);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }

  /**
   * <pre>
   * 1. ClassName :   FooterViewHolder
   * 2. FileName  :   InvitationListAdapter.java
   * 3. Package   :   com.ntels.onecable.view.invitation
   * 4. Date      :   2016-12-01
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   * </pre>
   */
  public static class FooterViewHolder extends RecyclerView.ViewHolder {

    public LinearLayout llFooterRoot;

    /**
     * 생성자.
     *
     * @param itemView Footer layout view
     */
    public FooterViewHolder(View itemView) {

      super(itemView);

      try {
        llFooterRoot = (LinearLayout) itemView.findViewById(R.id.llFooterRoot);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    try {
      HashMap<String, Object> itemMap;
      itemMap = alTotalList.get(position);

      if (holder instanceof ItemViewHolder) {
        ItemViewHolder itemViewHolderView = (ItemViewHolder) holder;

        String nickname = util.null2string(itemMap.get(Constants.KEY_NICKNAME));
        String imageFileName = util.null2string(itemMap.get(Constants.KEY_IMAGE_FILE_NAME));

        util.setRefreshImage(context, imageFileName, itemViewHolderView.ivBodyImage);
        itemViewHolderView.tvBodyBody.setText(nickname);
        itemViewHolderView.tvCmdOk.setTag(position);
        itemViewHolderView.tvCmdOk.setOnClickListener(new View.OnClickListener() {
          int tag = 0;

          @Override
          public void onClick(View view) {

            tag = (int) view.getTag();
            adapterCallback.onCallAction(1, tag);
          }
        });
        itemViewHolderView.tvCmdCancel.setTag(position);
        itemViewHolderView.tvCmdCancel.setOnClickListener(new View.OnClickListener() {
          int tag = 0;

          @Override
          public void onClick(View view) {

            tag = (int) view.getTag();
            adapterCallback.onCallAction(2, tag);
          }
        });
        util.getViewUnderline(itemViewHolderView.tvCmdOk);
        util.getViewUnderline(itemViewHolderView.tvCmdCancel);
      } else if (holder instanceof HeaderViewHolder) {

        HeaderViewHolder vhItemView = (HeaderViewHolder) holder;
      } else if (holder instanceof FooterViewHolder) {

        FooterViewHolder vhItemView = (FooterViewHolder) holder;
      }
      holder.itemView.setTag(itemMap);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public int getItemViewType(int position) {

    if (getHeaderCheck() && position == 0) {
      return TYPE_HEADER;
    } else if (getFooterCheck() && position == getItemCount() - 1) {
      return TYPE_FOOTER;
    } else {
      return TYPE_ITEM;
    }
  }

}
