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

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

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.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.common.AppConfig;
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 Fragment_HomeMember_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List_Interface mAdapterCallback;
    private AppConfig      appConfig;
    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              int     iType                          = 0;
    private static final int     MSG_COM_DEVICES_NODES_COMMANDS = 1;
    private              String  sHTTP_request_value            = "";
    private              String  sHTTP_device_id                = "";
    private              String  sHTTP_node_id                  = "";
    private              int     iHTTP_position                 = 0;
    private              String  sHTTP_value                    = "";
    private              Boolean bSend                          = false;
    private              Boolean bListCheck                     = false;
    private              Boolean bHeaderCheck                   = false;
    private              Boolean bFooterCheck                   = false;
    private              Boolean bFooterVisible                 = 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;
    }

    public void setFooterVisible(boolean bVal) {

        bFooterVisible = bVal;
    }

    public boolean getFooterVisible() {

        return bFooterVisible;
    }

    @Override public int getItemCount() {

        return alTotalList.size();
    }

    public Fragment_HomeMember_Adapter(Context tContext, int listLayoutHead, int listLayoutItem, int listLayoutFooter, ArrayList<HashMap<String, Object>> alTmp, List_Interface mAdapterCallback) {

        mContext = tContext;
        alTotalList = alTmp;
        mListLayoutHead = listLayoutHead;
        mListLayoutItem = listLayoutItem;
        mListLayoutFooter = listLayoutFooter;
        appConfig = (AppConfig) mContext.getApplicationContext();
        //		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        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 vhItem 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;

        public vhItem(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);
        }
    }

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

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public ImageView    ivThingsImage;
        public TextView     tvTitle;
        public LinearLayout llContents;
        public ImageView    ivArrow;

        public vhHeder(View itemView) {

            super(itemView);

            //			llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
            //			llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
            //			ivThingsImage = (ImageView) itemView.findViewById(R.id.ivThingsImage);
            //			tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            //			llContents = (LinearLayout) itemView.findViewById(R.id.llContents);
            //			ivArrow = (ImageView) itemView.findViewById(R.id.ivArrow);
        }
    }

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

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

        public vhFooter(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);
        }
    }

    /**
     * listView getView 를 대체 넘겨 받은 데이터를 화면에 출력하는 역할
     */
    @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_member_type_code = util.null2string(hmItem.get(Constants.HTTP_member_type_code));
                String sHTTP_invitation = util.null2string(hmItem.get(Constants.HTTP_invitation));
                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_grade_code = util.null2string(hmItem.get(Constants.HTTP_grade_code));
                String sHTTP_grade_code_name = util.null2string(hmItem.get(Constants.HTTP_grade_code_name));
                String sHTTP_member_id = util.null2string(hmItem.get(Constants.HTTP_member_id));
                String sHTTP_invitation_hist_id = util.null2string(hmItem.get(Constants.HTTP_invitation_hist_id));
                String sHTTP_CHECK_YN = util.null2string(hmItem.get(Constants.HTTP_CHECK_YN));

                util.setRefreshImage(mContext, sHTTP_image_file_name, vhItemView.ivRowIcon);

                vhItemView.ivRowBullet.setVisibility(View.GONE);
                if ( sHTTP_member_type_code.equals("01") ) {
                    vhItemView.ivRowBullet.setVisibility(View.VISIBLE);
                    vhItemView.tvRowTitle.setText(sHTTP_nickname);
                    vhItemView.tvRowBody.setSelected(true);
                    if ( sHTTP_grade_code.equals("MAS") ) {
                        vhItemView.ivRowBullet.setSelected(false);
                        vhItemView.tvRowBody.setText(util.getString(mContext, R.string.layout_master_user));
                    } else {
                        vhItemView.ivRowBullet.setSelected(true);
                        vhItemView.tvRowBody.setText(util.getString(mContext, R.string.layout_default_user));
                    }
                } else if ( sHTTP_member_type_code.equals("02") ) {
                    vhItemView.tvRowTitle.setText(sHTTP_nickname);
                    vhItemView.tvRowBody.setSelected(false);
                    if ( sHTTP_invitation.equals("01") ) {
                        vhItemView.tvRowBody.setText(util.getString(mContext, R.string.layout_invitation_member));
                    } else if ( sHTTP_invitation.equals("21") ) {
                        vhItemView.tvRowBody.setText(util.getString(mContext, R.string.layout_an_invitation_member));
                    } else if ( sHTTP_invitation.equals("22") ) {
                        vhItemView.tvRowBody.setText(util.getString(mContext, R.string.layout_an_invitation_member_not_pass));
                    } else if ( sHTTP_invitation.equals("23") ) {
                        vhItemView.tvRowBody.setText(util.getString(mContext, R.string.layout_an_invitation_member1));
                    }
                }

                vhItemView.ivCheck.setVisibility(View.GONE);
                vhItemView.ivCheck.setSelected(false);
                if ( bListCheck ) {
                    if ( !sHTTP_grade_code.equals("MAS") ) {
                        vhItemView.ivCheck.setVisibility(View.VISIBLE);
                        if ( sHTTP_CHECK_YN.equals("Y") ) {
                            vhItemView.ivCheck.setSelected(true);
                        }
                    }
                }
                vhItemView.llRowBody.setTag(position);
                vhItemView.llRowBody.setOnClickListener(new OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        if ( bListCheck ) {
                            if ( !alTotalList.get(iTag).get(Constants.HTTP_grade_code).equals("MAS") ) {
                                if ( alTotalList.get(iTag).get(Constants.HTTP_CHECK_YN).equals("Y") ) {
                                    alTotalList.get(iTag).put(Constants.HTTP_CHECK_YN, "N");
                                } else {
                                    alTotalList.get(iTag).put(Constants.HTTP_CHECK_YN, "Y");
                                }
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
            } else if ( holder instanceof vhFooter ) {

                vhFooter vhItemView = (vhFooter) holder;
                String sMore_Yn = util.null2string(hmItem.get(Constants.HTTP_MORE_YN));
                if ( sMore_Yn.equals("Y") ) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    if ( bListCheck ) {
                        vhItemView.llFooterBody.setVisibility(View.GONE);
                    } else {
                        if ( bFooterVisible ) {
                            vhItemView.llFooterBody.setVisibility(View.VISIBLE);
                        } else {
                            vhItemView.llFooterBody.setVisibility(View.GONE);
                        }

                        vhItemView.llFooterBody.setOnClickListener(new View.OnClickListener() {
                            String sTag = "";
                            private Intent intent;

                            @Override public void onClick(View v) {

                                mAdapterCallback.onCallAction(0, 0);
                            }
                        });
                    }
                }
            }
            holder.itemView.setTag(hmItem);
        } catch (Exception e) {
            e.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;
        }
    }
}
