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

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

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.Button;
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 Message_Center_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 Message_Center_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 ImageView    ivRowBody_Bullet;
        public TextView     tvRowBody_Title;
        public TextView     tvRowBody_Body;
        public Button       btnRowBody_Ok;
        public TextView     tvRowBody_Date;

        public vhItem(View itemView) {

            super(itemView);
            try {
                llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
                llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
                ivRowBody_Bullet = (ImageView) itemView.findViewById(R.id.ivRowBody_Bullet);
                tvRowBody_Title = (TextView) itemView.findViewById(R.id.tvRowBody_Title);
                tvRowBody_Body = (TextView) itemView.findViewById(R.id.tvRowBody_Body);
                btnRowBody_Ok = (Button) itemView.findViewById(R.id.btnRowBody_Ok);
                tvRowBody_Date = (TextView) itemView.findViewById(R.id.tvRowBody_Date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

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

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

        public vhFooter(View itemView) {

            super(itemView);

            try {
                llFooterProgress = (LinearLayout) itemView.findViewById(R.id.llFooterProgress);
                llFooterRoot = (LinearLayout) itemView.findViewById(R.id.llFooterRoot);
                llFooterBody = (LinearLayout) itemView.findViewById(R.id.llFooterBody);
                tvFooterTitle = (TextView) itemView.findViewById(R.id.tvFooterTitle);
                ivFooterAdd = (ImageView) itemView.findViewById(R.id.ivFooterAdd);
            } 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_push_hist_id = util.null2string(hmItem.get(Constants.HTTP_push_noti_hist_id));
                String sHTTP_content = util.null2string(hmItem.get(Constants.HTTP_content));
                String sHTTP_create_datetime = util.null2string(hmItem.get(Constants.HTTP_create_datetime));
                String sHTTP_read_datetime = util.null2string(hmItem.get(Constants.HTTP_read_datetime));
                String sHTTP_homegrp_invitation_yn = util.null2string(hmItem.get(Constants.HTTP_homegrp_invitation_yn));

                if ( sHTTP_read_datetime.equals("") ) {
                    vhItemView.ivRowBody_Bullet.setSelected(true);
                } else {
                    vhItemView.ivRowBody_Bullet.setSelected(false);
                }
                vhItemView.tvRowBody_Title.setText(sHTTP_content);
                vhItemView.tvRowBody_Date.setText(util.UTCtoLocal5(sHTTP_create_datetime));
                //                vhItemView.tvRowBody_Body.setText(sHTTP_content);
                vhItemView.tvRowBody_Body.setVisibility(View.GONE);
                if ( sHTTP_homegrp_invitation_yn.equals("") ) {
                    vhItemView.btnRowBody_Ok.setVisibility(View.GONE);
                } else if ( sHTTP_homegrp_invitation_yn.equals("N") ) {
                    vhItemView.btnRowBody_Ok.setVisibility(View.VISIBLE);
                    vhItemView.btnRowBody_Ok.setEnabled(false);
                } else if ( sHTTP_homegrp_invitation_yn.equals("Y") ) {
                    vhItemView.btnRowBody_Ok.setVisibility(View.VISIBLE);
                    vhItemView.btnRowBody_Ok.setEnabled(true);
                }
                vhItemView.btnRowBody_Ok.setTag(position);
                vhItemView.btnRowBody_Ok.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        mAdapterCallback.onCallAction(1, iTag);
                    }
                });
            } else if ( holder instanceof vhHeder ) {

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

                vhFooter vhItemView = (vhFooter) holder;

                String sMore_Yn = util.null2string(hmItem.get(Constants.HTTP_MORE_YN));

                vhItemView.llFooterBody.setVisibility(View.GONE);
                //                vhItemView.llFooterRoot.setBackgroundResource(R.drawable.Translucent);
                if ( sMore_Yn.equals("Y") ) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                    vhItemView.llFooterRoot.setVisibility(View.VISIBLE);
                    //                    if ( (position % 2) == 0 ) {
                    //                        vhItemView.llFooterRoot.setBackgroundResource(R.drawable.img_list_bg_02);
                    //                    } else {
                    //                        vhItemView.llFooterRoot.setBackgroundResource(R.drawable.img_list_bg_01);
                    //                    }
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    //                    vhItemView.llFooterRoot.setBackgroundResource(R.drawable.Translucent);
                    vhItemView.llFooterRoot.setVisibility(View.GONE);
                }
            }
            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;
    }
}
