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

package com.ntels.onecable.admin.view.mainmore;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.c_interface.ListInterface;
import com.ntels.onecable.admin.common.util.Util;

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 java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * <pre>
 * 1. ClassName :   HomehubListAdapter
 * 2. FileName  :   HomehubListAdapter.java
 * 3. Package   :   com.ntels.onecable.admin.view.mainmore
 * 4. Date      :   2017-05-16
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class HomehubListAdapter 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 Util mUtil = new Util();
    private ArrayList<HashMap<String, Object>> mAlTotalList = new ArrayList<>();
    private ListInterface mAdapterCallback;
    private int mListLayoutHeader;
    private int mListLayoutItem;
    private int mListLayoutFooter;
    private Boolean mIsListCheck = false;
    private Boolean mIsHeaderCheck = false;
    private Boolean mIsFooterCheck = false;

    /**
     * 생성자.
     *
     * @param headerLayoutItem Header layout resource id
     * @param listLayoutItem   Item layout resource id
     * @param footerLayoutItem Footer layout resource id
     * @param alTmp            RecyclerView에 보여줄 data
     */
    public HomehubListAdapter(int headerLayoutItem, int listLayoutItem, int footerLayoutItem, ArrayList<HashMap<String, Object>> alTmp,
                              ListInterface listInterface) {

        mAlTotalList = alTmp;
        mListLayoutHeader = headerLayoutItem;
        this.mListLayoutItem = listLayoutItem;
        mListLayoutFooter = footerLayoutItem;
        mAdapterCallback = listInterface;
    }

    /**
     * {@link #mIsListCheck} getter.
     *
     * @return true or false
     */
    public boolean getListCheck() {

        return mIsListCheck;
    }

    /**
     * {@link #mIsListCheck} setter.
     *
     * @param isCheck true or false
     */
    public void setListCheck(boolean isCheck) {

        mIsListCheck = isCheck;
    }

    /**
     * {@link #mIsHeaderCheck} getter.
     *
     * @return true or false
     */
    public boolean getHeaderCheck() {

        return mIsHeaderCheck;
    }

    /**
     * {@link #mIsHeaderCheck} setter.
     *
     * @param isCheck true or false
     */
    public void setHeaderCheck(boolean isCheck) {

        mIsHeaderCheck = isCheck;
    }

    /**
     * {@link #mIsFooterCheck} getter.
     *
     * @return true or false
     */
    public boolean getFooterCheck() {

        return mIsFooterCheck;
    }

    /**
     * {@link #mIsFooterCheck} setter.
     *
     * @param isCheck true or false
     */
    public void setFooterCheck(boolean isCheck) {

        mIsFooterCheck = isCheck;
    }

    @Override
    public int getItemCount() {

        return mAlTotalList.size();
    }

    public void setData(ArrayList<HashMap<String, Object>> alTmp) {
        this.mAlTotalList = alTmp;
    }

    @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 ItemViewHolder(view);
        } else if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutHeader, parent, false);
            return new HeaderViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutFooter, parent, false);
            return new FooterViewHolder(view);
        }

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

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

        try {
            HashMap<String, Object> itemMap;
            itemMap = mAlTotalList.get(position);

            if (holder instanceof ItemViewHolder) {
                ItemViewHolder itemViewHolderView = (ItemViewHolder) holder;

                String homehubUserName = mUtil.null2string(itemMap.get(Constants.KEY_PRD_USER_NAME));
                String homehubName = mUtil.null2string(itemMap.get(Constants.KEY_PRD_NAME));
                String deviceOnline = mUtil.null2string(itemMap.get(Constants.KEY_DEVICE_ONLINE));
                String deviceConn = mUtil.null2string(itemMap.get(Constants.KEY_DEVICE_CONN));
                String softwareVersion = mUtil.null2string(itemMap.get(Constants.KEY_SOFTWARE_VERSION));
                String lastSoftwareVersion = mUtil.null2string(itemMap.get(Constants.KEY_LAST_SOFTWARE_VERSION));
                String firmwareVersion = mUtil.null2string(itemMap.get(Constants.KEY_LAST_FIRMWARE_VERSION));
                String lastFirmwareVersion = mUtil.null2string(itemMap.get(Constants.KEY_LAST_FIRMWARE_VERSION));

                if ("".equals(homehubUserName)) {
                    itemViewHolderView.rowHomehubName.setText(homehubName);
                } else {
                    itemViewHolderView.rowHomehubName.setText(homehubUserName);
                }
                if (deviceConn.equalsIgnoreCase("connect")) {
                    itemViewHolderView.rowNetworkStatusLayout.setSelected(true);
                } else {
                    itemViewHolderView.rowNetworkStatusLayout.setSelected(false);
                }
                if (deviceOnline.equalsIgnoreCase("on")) {
                    itemViewHolderView.rowZwaveStatusLayout.setSelected(true);
                } else {
                    itemViewHolderView.rowZwaveStatusLayout.setSelected(false);
                }
                if (!softwareVersion.equals(lastSoftwareVersion) || !firmwareVersion.equals(lastFirmwareVersion)) {
                    itemViewHolderView.rowUpdateStatusLayout.setSelected(true);
                } else {
                    itemViewHolderView.rowUpdateStatusLayout.setSelected(false);
                }

                itemViewHolderView.rowHomehubNameLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mAdapterCallback.onCallAction(0, position);
                    }
                });
            } else if (holder instanceof HeaderViewHolder) {
                // TODO: 2016-12-01 HeaderViewHolder 셋팅.
            } else if (holder instanceof FooterViewHolder) {

            }
            holder.itemView.setTag(itemMap);
        } 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;
        }
    }

    /**
     * <pre>
     * 1. ClassName :   HeaderViewHolder
     * 2. FileName  :   HomehubListAdapter.java
     * 3. Package   :   com.ntels.onecable.admin.view.mainmore
     * 4. Date      :   2017-05-16
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {

        /**
         * 생성자.
         *
         * @param itemView Header layout view.
         */
        public HeaderViewHolder(View itemView) {

            super(itemView);

        }
    }

    /**
     * <pre>
     * 1. ClassName :   ItemViewHolder
     * 2. FileName  :   HomehubListAdapter.java
     * 3. Package   :   com.ntels.onecable.admin.view.mainmore
     * 4. Date      :   2017-05-16
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.llRowRoot) LinearLayout rowRootLayout;
        @BindView(R.id.llHomehubName) LinearLayout rowHomehubNameLayout;
        @BindView(R.id.llNetworkStatus) LinearLayout rowNetworkStatusLayout;
        @BindView(R.id.llZwaveStatus) LinearLayout rowZwaveStatusLayout;
        @BindView(R.id.llUpdateStatus) LinearLayout rowUpdateStatusLayout;
        @BindView(R.id.tvHomehubName) TextView rowHomehubName;

        /**
         * 생성자.
         *
         * @param itemView Item layout view.
         */
        public ItemViewHolder(View itemView) {

            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * <pre>
     * 1. ClassName :   FooterViewHolder
     * 2. FileName  :   HomehubListAdapter.java
     * 3. Package   :   com.ntels.onecable.admin.view.mainmore
     * 4. Date      :   2017-05-16
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class FooterViewHolder extends RecyclerView.ViewHolder {

        /**
         * Footer layout view.
         *
         * @param itemView Footer layout view
         */
        public FooterViewHolder(View itemView) {

            super(itemView);
        }
    }
}
