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

package com.ntels.onecable.view.rules;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
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.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

/**
 * 1. ClassName :   FragmentRulesAdapter
 * 2. FileName  :   FragmentRulesAdapter.java
 * 3. Package   :   com.ntels.onecable.view.rules
 * 4. Date      :   2016-11-28
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 */
public class FragmentRulesAdapter 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 AppConfig appConfig;
  private Util util = new Util();
  private ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<HashMap<String, Object>>();
  private Context context;
  private int listLayoutHead;
  private int listLayoutItem;
  private int listLayoutFooter;
  private Boolean isListCheck = false;
  private Boolean isHeaderCheck = false;
  private Boolean isFooterCheck = false;
  private Boolean isFooterVisible = false;

  public void setListCheck(boolean isCheck) {

    isListCheck = isCheck;
  }

  public boolean getListCheck() {

    return isListCheck;
  }

  public void setHeaderCheck(boolean isCheck) {

    isHeaderCheck = isCheck;
  }

  public boolean getHeaderCheck() {

    return isHeaderCheck;
  }

  public void setFooterCheck(boolean isCheck) {

    isFooterCheck = isCheck;
  }

  public boolean getFooterCheck() {

    return isFooterCheck;
  }

  public void setFooterVisible(boolean isVisible) {

    isFooterVisible = isVisible;
  }

  public boolean getFooterVisible() {

    return isFooterVisible;
  }

  @Override
  public int getItemCount() {

    return alTotalList.size();
  }

  /**
   * 생성자.
   *
   * @param context : 컨텍스트
   * @param listLayoutHead : Header resource id
   * @param listLayoutItem : Item resource id
   * @param listLayoutFooter : Footer resource id
   * @param alTmp : adapter에 표현 할 data
   * @param adapterCallback : callback listener
   */
  public FragmentRulesAdapter(Context context, int listLayoutHead, int listLayoutItem,
                              int listLayoutFooter, ArrayList<HashMap<String, Object>> alTmp,
                              ListInterface adapterCallback) {

    this.context = context;
    alTotalList = alTmp;
    this.listLayoutHead = listLayoutHead;
    this.listLayoutItem = listLayoutItem;
    this.listLayoutFooter = listLayoutFooter;
    appConfig = (AppConfig) this.context.getApplicationContext();

    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(listLayoutHead, 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");
  }

  /**
   * 1. ClassName :   ItemViewHolder
   * 2. FileName  :   FragmentRulesAdapter.java
   * 3. Package   :   com.ntels.onecable.view.rules
   * 4. Date      :   2016-11-28
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   */
  public static class ItemViewHolder extends RecyclerView.ViewHolder {

    public LinearLayout llRowRoot;
    public LinearLayout llRowBody;
    public ImageView ivRowBullet;
    public ImageView ivRowIcon;
    public TextView tvRowTitle;
    public Button btnRowOnOff;
    public Button btnRowDelete;

    /**
     * 생성자.
     *
     * @param itemView : Item view holder에 사용 할 view.
     */
    public ItemViewHolder(View itemView) {

      super(itemView);
      llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
      llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
      ivRowBullet = (ImageView) itemView.findViewById(R.id.ivRowBullet);
      ivRowIcon = (ImageView) itemView.findViewById(R.id.ivRowIcon);
      tvRowTitle = (TextView) itemView.findViewById(R.id.tvRowTitle);
      btnRowOnOff = (Button) itemView.findViewById(R.id.btnRowOnOff);
      btnRowDelete = (Button) itemView.findViewById(R.id.btnRowDelete);
    }
  }

  /**
   * 1. ClassName :   HeaderViewHolder
   * 2. FileName  :   FragmentRulesAdapter.java
   * 3. Package   :   com.ntels.onecable.view.rules
   * 4. Date      :   2016-11-28
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   */
  public static class HeaderViewHolder extends RecyclerView.ViewHolder {

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

    /**
     * 생성자.
     *
     * @param itemView : Header view holder에 사용 할 view.
     */
    public HeaderViewHolder(View itemView) {

      super(itemView);
    }
  }

  /**
   * 1. ClassName :   FooterViewHolder
   * 2. FileName  :   FragmentRulesAdapter.java
   * 3. Package   :   com.ntels.onecable.view.rules
   * 4. Date      :   2016-11-28
   * 5. Author    :   aaron
   * 6. Comment   :   Initialize
   */
  public static class FooterViewHolder extends RecyclerView.ViewHolder {

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

    /**
     * 생성자.
     *
     * @param itemView : Footer view holder에 사용 할 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);
      lineView = (View) itemView.findViewById(R.id.vLine);
      ivFooterAdd = (ImageView) itemView.findViewById(R.id.ivFooterAdd);
    }
  }

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

    try {
      HashMap<String, Object> hmItem = new HashMap<String, Object>();
      hmItem = alTotalList.get(position);
      if (holder instanceof ItemViewHolder) {
        ItemViewHolder itemViewHolderView = (ItemViewHolder) holder;

        // 규칙 명
        String ruleName = util.null2string(hmItem.get(Constants.KEY_RULE_NAME));
        // 사용여부(Y:사용, N:미사용, X:실행불가능)
        String useYn = util.null2string(hmItem.get(Constants.KEY_USE_YN));
        // 이미지 파일 URL
        String iamgeFileName = util.null2string(hmItem.get(Constants.KEY_IMAGE_FILE_NAME));

        util.setImage(context, iamgeFileName, itemViewHolderView.ivRowIcon);

        itemViewHolderView.tvRowTitle.setText(ruleName);

        if (useYn.equals("Y")) {
          itemViewHolderView.llRowBody.setSelected(true);
        } else {
          itemViewHolderView.llRowBody.setSelected(false);
        }
        if (isListCheck) {
          itemViewHolderView.btnRowOnOff.setVisibility(View.GONE);
          itemViewHolderView.btnRowDelete.setVisibility(View.VISIBLE);
        } else {
          itemViewHolderView.btnRowDelete.setVisibility(View.GONE);
          if (appConfig.getSelect_Homegrp_Grade_Code().equals("MAS")) {
            itemViewHolderView.btnRowOnOff.setVisibility(View.VISIBLE);
          } else {
            itemViewHolderView.btnRowOnOff.setVisibility(View.GONE);
          }
        }

        // 행 클릭
        itemViewHolderView.llRowBody.setTag(position);
        itemViewHolderView.llRowBody.setOnClickListener(new View.OnClickListener() {
          int tag = 0;

          @Override
          public void onClick(View view) {

            tag = (int) view.getTag();
            if (isListCheck) {
              adapterCallback.onCallAction(2, tag);
            } else {
              adapterCallback.onCallAction(1, tag);
            }
          }
        });
        // 삭제
        itemViewHolderView.btnRowDelete.setTag(position);
        itemViewHolderView.btnRowDelete.setOnClickListener(new View.OnClickListener() {
          int tag = 0;

          @Override
          public void onClick(View view) {

            tag = (int) view.getTag();
            if (isListCheck) {
              adapterCallback.onCallAction(2, tag);
            }
          }
        });
        // 규칙활성화 및 비활성화
        itemViewHolderView.btnRowOnOff.setTag(position);
        itemViewHolderView.btnRowOnOff.setOnClickListener(new View.OnClickListener() {
          int tag = 0;

          @Override
          public void onClick(View view) {

            tag = (int) view.getTag();
            if (!isListCheck) {
              adapterCallback.onCallAction(3, tag);
            }
          }
        });
      } else if (holder instanceof FooterViewHolder) {

        FooterViewHolder vhItemView = (FooterViewHolder) holder;
        String moreYn = util.null2string(hmItem.get(Constants.KEY_MORE_YN));
        if (moreYn.equals("Y")) {
          vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
          vhItemView.lineView.setVisibility(View.VISIBLE);
        } else {
          vhItemView.llFooterProgress.setVisibility(View.GONE);
          vhItemView.lineView.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(hmItem);
    } 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;
    }
  }
}
