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

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

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.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_Rules_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_Rules_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 ImageView    ivRowBullet;
        public ImageView    ivRowIcon;
        public TextView     tvRowTitle;
        public TextView     tvRowControl;
        public Button       btnRowOnOff;
        public Button       btnRowDelete;

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

    /**
     * 뷰 재활용을 위한 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 View         vLine;
        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);
            vLine = (View) itemView.findViewById(R.id.vLine);
            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;

                /**홈그룹 규칙 ID*/
                String sHTTP_homegrp_rule_id = util.null2string(hmItem.get(Constants.HTTP_homegrp_rule_id));
                /**규칙 ID*/
                String sHTTP_rule_id = util.null2string(hmItem.get(Constants.HTTP_rule_id));
                /**규칙 명*/
                String sHTTP_rule_name = util.null2string(hmItem.get(Constants.HTTP_rule_name));
                /**사용여부(Y:사용, N:미사용, X:실행불가능)*/
                String sHTTP_use_yn = util.null2string(hmItem.get(Constants.HTTP_use_yn));
                /**이미지 파일 URL*/
                String sHTTP_image_file_name = util.null2string(hmItem.get(Constants.HTTP_image_file_name));
                /**(생성자 회원)닉네임*/
                String sHTTP_nickname = util.null2string(hmItem.get(Constants.HTTP_nickname));
                /**생성일시 ex)20150206042252*/
                String sHTTP_create_datetime = util.null2string(hmItem.get(Constants.HTTP_create_datetime));

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

                vhItemView.tvRowTitle.setText(sHTTP_rule_name);

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

                /*행 클릭*/
                vhItemView.llRowBody.setTag(position);
                vhItemView.llRowBody.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        if ( bListCheck ) {
                            mAdapterCallback.onCallAction(2, iTag);
                        } else {
                            mAdapterCallback.onCallAction(1, iTag);
                        }
                    }
                });
                /*삭제*/
                vhItemView.btnRowDelete.setTag(position);
                vhItemView.btnRowDelete.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        if ( bListCheck ) {
                            mAdapterCallback.onCallAction(2, iTag);
                        }
                    }
                });
                /*규칙활성화 및 비활성화*/
                vhItemView.btnRowOnOff.setTag(position);
                vhItemView.btnRowOnOff.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        if ( !bListCheck ) {
                            mAdapterCallback.onCallAction(3, iTag);
                        }
                    }
                });
            } 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);
                    vhItemView.vLine.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    vhItemView.vLine.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) {
        }
    }

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