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

package com.ntels.kneet.v3.view.homemember;

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

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

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

/**
 * <pre>
 * 1. ClassName :   FragmentHomeMemberAdapter
 * 2. FileName  :   FragmentHomeMemberAdapter.java
 * 3. Package   :   com.ntels.kneet.v3.view.homemember
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class FragmentHomeMemberAdapter 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 int type = 0;
  private Boolean isListCheck = false;
  private Boolean isHeaderCheck = false;
  private Boolean isFooterCheck = false;
  private Boolean isFooterVisible = 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;
  }

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

    isFooterVisible = isCheck;
  }

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

    return isFooterVisible;
  }

  @Override
  public int getItemCount() {

    return alTotalList.size();
  }

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

    this.context = context;
    alTotalList = alTmp;
    this.listLayoutHeader = listLayoutHeader;
    this.listLayoutItem = listLayoutItem;
    this.listLayoutFooter = listLayoutFooter;

    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 ItemViewHoler(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 :   ItemViewHoler
   * 2. FileName  :   FragmentHomeMemberAdapter.java
   * 3. Package   :   com.ntels.kneet.v3.view.homemember
   * 4. Date      :   2016-12-01
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   * </pre>
   */
  public static class ItemViewHoler extends RecyclerView.ViewHolder {

    public LinearLayout llRowRoot;
    public LinearLayout llRowBody;
    public RelativeLayout rlRowBody;
    public ImageView ivRowIcon;
    public ImageView ivRowBullet;
    public TextView tvRowTitle;
    public TextView tvRowBody;
    public ImageView ivCheck;

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

      super(itemView);
      llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
      llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
      rlRowBody = (RelativeLayout) itemView.findViewById(R.id.rlRowBody);
      ivRowIcon = (ImageView) itemView.findViewById(R.id.ivRowIcon);
      ivRowBullet = (ImageView) itemView.findViewById(R.id.ivRowBullet);
      tvRowTitle = (TextView) itemView.findViewById(R.id.tvRowTitle);
      tvRowBody = (TextView) itemView.findViewById(R.id.tvRowBody);
      ivCheck = (ImageView) itemView.findViewById(R.id.ivCheck);
    }
  }

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

    public LinearLayout llRowRoot;
    public LinearLayout llRowBody;
    public TextView tvTitle;

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

      super(itemView);

    }
  }

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

    public LinearLayout llFooterProgress;
    public LinearLayout llFooterRoot;
    public LinearLayout llFooterBody;
    public ImageView ivFooterAdd;

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

      super(itemView);

      llFooterProgress = (LinearLayout) itemView.findViewById(R.id.llFooterProgress);
      llFooterRoot = (LinearLayout) itemView.findViewById(R.id.llFooterRoot);
      llFooterBody = (LinearLayout) itemView.findViewById(R.id.llFooterBody);
      ivFooterAdd = (ImageView) itemView.findViewById(R.id.ivFooterAdd);
    }
  }

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

    try {
      HashMap<String, Object> itemMap;
      itemMap = alTotalList.get(position);
      if (holder instanceof ItemViewHoler) {
        ItemViewHoler itemViewHolerView = (ItemViewHoler) holder;

        String memberTypeCode = util.null2string(itemMap.get(Constants.HTTP_member_type_code));
        String invitation = util.null2string(itemMap.get(Constants.HTTP_invitation));
        String nickname = util.null2string(itemMap.get(Constants.HTTP_nickname));
        String imageFileName = util.null2string(itemMap.get(Constants.HTTP_image_file_name));
        String gradeCode = util.null2string(itemMap.get(Constants.HTTP_grade_code));
        String checkYn = util.null2string(itemMap.get(Constants.HTTP_CHECK_YN));

        util.setRefreshImage(context, imageFileName, itemViewHolerView.ivRowIcon);

        itemViewHolerView.ivRowBullet.setVisibility(View.GONE);
        if (memberTypeCode.equals("01")) {
          itemViewHolerView.ivRowBullet.setVisibility(View.VISIBLE);
          itemViewHolerView.tvRowTitle.setText(nickname);
          itemViewHolerView.tvRowBody.setSelected(true);
          if (gradeCode.equals("MAS")) {
            itemViewHolerView.ivRowBullet.setSelected(false);
            itemViewHolerView.tvRowBody
                .setText(util.getString(context, R.string.layout_master_user));
          } else {
            itemViewHolerView.ivRowBullet.setSelected(true);
            itemViewHolerView.tvRowBody
                .setText(util.getString(context, R.string.layout_default_user));
          }
        } else if (memberTypeCode.equals("02")) {
          itemViewHolerView.tvRowTitle.setText(nickname);
          itemViewHolerView.tvRowBody.setSelected(false);
          switch (invitation) {
            case "01":
              itemViewHolerView.tvRowBody
                  .setText(util.getString(context, R.string.layout_invitation_member));
              break;
            case "21":
              itemViewHolerView.tvRowBody
                  .setText(util.getString(context, R.string.layout_an_invitation_member));
              break;
            case "22":
              itemViewHolerView.tvRowBody
                  .setText(util.getString(context, R.string.layout_an_invitation_member_not_pass));
              break;
            case "23":
              itemViewHolerView.tvRowBody
                  .setText(util.getString(context, R.string.layout_an_invitation_member1));
              break;
            default:
              break;
          }
        }

        itemViewHolerView.ivCheck.setVisibility(View.GONE);
        itemViewHolerView.ivCheck.setSelected(false);
        if (isListCheck) {
          if (!gradeCode.equals("MAS")) {
            itemViewHolerView.ivCheck.setVisibility(View.VISIBLE);
            if (checkYn.equals("Y")) {
              itemViewHolerView.ivCheck.setSelected(true);
            }
          }
        }
        itemViewHolerView.llRowBody.setTag(position);
        itemViewHolerView.llRowBody.setOnClickListener(new OnClickListener() {
          int tag = 0;

          @Override
          public void onClick(View view) {

            tag = (int) view.getTag();
            if (isListCheck) {
              if (!alTotalList.get(tag).get(Constants.HTTP_grade_code).equals("MAS")) {
                if (alTotalList.get(tag).get(Constants.HTTP_CHECK_YN).equals("Y")) {
                  alTotalList.get(tag).put(Constants.HTTP_CHECK_YN, "N");
                } else {
                  alTotalList.get(tag).put(Constants.HTTP_CHECK_YN, "Y");
                }
                notifyDataSetChanged();
              }
            }
          }
        });
      } else if (holder instanceof FooterViewHolder) {

        FooterViewHolder vhItemView = (FooterViewHolder) holder;
        String moreYn = util.null2string(itemMap.get(Constants.HTTP_MORE_YN));
        if (moreYn.equals("Y")) {
          vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
        } else {
          vhItemView.llFooterProgress.setVisibility(View.GONE);
          if (isListCheck) {
            vhItemView.llFooterBody.setVisibility(View.GONE);
          } else {
            if (isFooterVisible) {
              vhItemView.llFooterBody.setVisibility(View.VISIBLE);
            } else {
              vhItemView.llFooterBody.setVisibility(View.GONE);
            }

            vhItemView.llFooterBody.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View view) {

                adapterCallback.onCallAction(0, 0);
              }
            });
          }
        }
      }
      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;
    }
  }
}
