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

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

import android.content.Context;
import android.content.Intent;
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 com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.c_interface.List_Interface;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.R;

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

public class Invitation_List_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List_Interface mAdapterCallback;

    private             Util                               util        = new Util();
    public static final int                                TYPE_HEADER = 0;
    public static final int                                TYPE_ITEM   = 1;
    public static final int                                TYPE_FOOTER = 2;
    private             ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<HashMap<String, Object>>();
    private LayoutInflater inflater;
    private Context        mContext;
    private int            mListLayoutHead;
    private int            mListLayoutItem;
    private int            mListLayoutFooter;
    private              Boolean bSend                   = false;
    private              String  sHTTP_dvcgrp_id         = "";
    private              String  sHTTP_device_id         = "";
    private              int     iType                   = 0;
    private              int     iSetType                = 0;
    private static final int     MSG_COM_GROUPS_DEVICES2 = 1;
    private              Boolean bListCheck              = false;
    private              Boolean bHeaderCheck            = false;
    private              Boolean bFooterCheck            = false;
    private              int     iFooterCnt              = -1;

    public void setListCheck(boolean bVal) {

        bListCheck = bVal;
    }

    public boolean getListCheck() {

        return bListCheck;
    }

    public void setHeaderCheck(boolean bVal) {

        bHeaderCheck = bVal;
    }

    public boolean getHeaderCheck() {

        return bHeaderCheck;
    }

    public void setFooterCheck(boolean bVal) {

        bFooterCheck = bVal;
    }

    public boolean getFooterCheck() {

        return bFooterCheck;
    }

    @Override public int getItemCount() {

        return alTotalList.size();
    }

    public Invitation_List_Adapter(Context tContext, int headerLayoutItem, int listLayoutItem, int footerLayoutItem, ArrayList<HashMap<String, Object>> alTmp, List_Interface mAdapterCallback) {

        mContext = tContext;
        alTotalList = alTmp;
        mListLayoutHead = headerLayoutItem;
        mListLayoutItem = listLayoutItem;
        mListLayoutFooter = footerLayoutItem;
        try {
            this.mAdapterCallback = ((List_Interface) mAdapterCallback);
        } catch (ClassCastException e) {
            e.printStackTrace();
            //            throw new ClassCastException("Activity must implement AdapterCallback.");
        }
    }

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

        if ( viewType == TYPE_ITEM ) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutItem, parent, false);
            return new vhItem(view);
        } else if ( viewType == TYPE_HEADER ) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutHead, parent, false);
            return new vhHeder(view);
        } else if ( viewType == TYPE_FOOTER ) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutFooter, parent, false);
            return new vhFooter(view);
        }

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

    /**
     * 뷰 재활용을 위한 viewHolder
     */
    public static class vhHeder extends RecyclerView.ViewHolder {

        public LinearLayout llHeaderRoot;
        public LinearLayout llHeaderBody;

        public vhHeder(View itemView) {

            super(itemView);

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

    public static class vhItem extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public LinearLayout llRowBody_Body;
        public LinearLayout llRowBody_Cmd;
        public ImageView    ivBodyImage;
        public TextView     tvBodyTitle;
        public TextView     tvBodyBody;
        public TextView     tvCmdOk;
        public TextView     tvCmdCancel;

        public vhItem(View itemView) {

            super(itemView);
            try {
                llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
                llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
                llRowBody_Body = (LinearLayout) itemView.findViewById(R.id.llRowBody_Body);
                llRowBody_Cmd = (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();
            }
        }
    }

    /**
     * 뷰 재활용을 위한 viewHolder
     */
    public static class vhFooter extends RecyclerView.ViewHolder {

        public LinearLayout llFooterRoot;
//        public LinearLayout llFooterBody;

        public vhFooter(View itemView) {

            super(itemView);

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

    @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 vhItem ) {
                vhItem vhItemView = (vhItem) holder;

                String sHTTP_homegrp_id = util.null2string(hmItem.get(Constants.HTTP_homegrp_id));
                String sHTTP_target_email = util.null2string(hmItem.get(Constants.HTTP_target_email));
                String sHTTP_nickname = util.null2string(hmItem.get(Constants.HTTP_nickname));
                String sHTTP_image_file_name = util.null2string(hmItem.get(Constants.HTTP_image_file_name));
                String sHTTP_homegrp_name = util.null2string(hmItem.get(Constants.HTTP_homegrp_name));
                String sHTTP_authorization_question = util.null2string(hmItem.get(Constants.HTTP_authorization_question));

                util.setRefreshImage(mContext, sHTTP_image_file_name, vhItemView.ivBodyImage);
                vhItemView.tvBodyBody.setText(sHTTP_nickname);
                vhItemView.tvCmdOk.setTag(position);
                vhItemView.tvCmdOk.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        mAdapterCallback.onCallAction(1, iTag);
                    }
                });
                vhItemView.tvCmdCancel.setTag(position);
                vhItemView.tvCmdCancel.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        mAdapterCallback.onCallAction(2, iTag);
                    }
                });
                util.getViewUnderline(vhItemView.tvCmdOk);
                util.getViewUnderline(vhItemView.tvCmdCancel);
            } else if ( holder instanceof vhHeder ) {

                vhHeder vhItemView = (vhHeder) holder;
            } else if ( holder instanceof vhFooter ) {

                vhFooter vhItemView = (vhFooter) holder;
            }
            holder.itemView.setTag(hmItem);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private boolean isPositionHeader(int position) {

        return position == 0;
    }

    @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;
        }
    }

    public ArrayList<HashMap<String, Object>> getAlhmVal() {

        return alTotalList;
    }
}
