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

package com.ntels.onecable.view.mainmore;

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

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

/**
 * <pre>
 * 1. ClassName :   MessageCenterAdapter
 * 2. FileName  :   MessageCenterAdapter.java
 * 3. Package   :   com.ntels.onecable.view.mainmore
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class MessageCenterAdapter 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 ListInterface adapterCallback;

    private Util util = new Util();
    private ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<>();
    private int listLayoutHeader;
    private int listLayoutItem;
    private int listLayoutFooter;
    private Boolean isListCheck = false;
    private Boolean isHeaderCheck = false;
    private Boolean isFooterCheck = false;

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

        alTotalList = alTmp;
        listLayoutHeader = headerLayoutItem;
        this.listLayoutItem = listLayoutItem;
        listLayoutFooter = footerLayoutItem;
        try {
            this.adapterCallback = adapterCallback;
        } catch (ClassCastException ex) {
            ex.printStackTrace();
        }
    }

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

        return isListCheck;
    }

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

        isListCheck = isCheck;
    }

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

        return isHeaderCheck;
    }

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

        isHeaderCheck = isCheck;
    }

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

        return isFooterCheck;
    }

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

        isFooterCheck = isCheck;
    }

    @Override
    public int getItemCount() {

        return alTotalList.size();
    }

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

        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutItem, parent, false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutHeader, parent, false);
            return new HeaderViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutFooter, 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, int position) {

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

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

                String content = util.null2string(itemMap.get(Constants.KEY_CONTENT));
                String createDatetime = util.null2string(itemMap.get(Constants.KEY_CREATE_DATETIME));
                String readDatetime = util.null2string(itemMap.get(Constants.KEY_READ_DATETIME));

                if (readDatetime.equals("")) {
                    itemViewHolderView.rowBodyBullet.setSelected(true);
                } else {
                    itemViewHolderView.rowBodyBullet.setSelected(false);
                }
                itemViewHolderView.rowBodyTitle.setText(content);
                itemViewHolderView.rowBodyDate.setText(util.UTCtoLocal5(createDatetime));
                itemViewHolderView.rowBodyBody.setVisibility(View.GONE);

                String homegrpInvitationYn = util.null2string(
                        itemMap.get(Constants.KEY_HOMEGRP_INVITATION_YN));
                if (homegrpInvitationYn.equals("")) {
                    itemViewHolderView.btnRowBodyOk.setVisibility(View.GONE);
                } else if (homegrpInvitationYn.equals("N")) {
                    itemViewHolderView.btnRowBodyOk.setVisibility(View.VISIBLE);
                    itemViewHolderView.btnRowBodyOk.setEnabled(false);
                } else if (homegrpInvitationYn.equals("Y")) {
                    itemViewHolderView.btnRowBodyOk.setVisibility(View.VISIBLE);
                    itemViewHolderView.btnRowBodyOk.setEnabled(true);
                }
                itemViewHolderView.btnRowBodyOk.setTag(position);
                itemViewHolderView.btnRowBodyOk.setOnClickListener(new View.OnClickListener() {
                    int tag = 0;

                    @Override
                    public void onClick(View view) {

                        tag = (int) view.getTag();
                        adapterCallback.onCallAction(1, tag);
                    }
                });
            } else if (holder instanceof HeaderViewHolder) {

                HeaderViewHolder vhItemView = (HeaderViewHolder) holder;
            } else if (holder instanceof FooterViewHolder) {

                FooterViewHolder vhItemView = (FooterViewHolder) holder;

                String moreYn = util.null2string(itemMap.get(Constants.KEY_MORE_YN));

                vhItemView.llFooterBody.setVisibility(View.GONE);
                if (moreYn.equals("Y")) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                    vhItemView.llFooterRoot.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    vhItemView.llFooterRoot.setVisibility(View.GONE);
                }
            }
            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  :   MessageCenterAdapter.java
     * 3. Package   :   com.ntels.onecable.view.mainmore
     * 4. Date      :   2016-12-01
     * 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  :   MessageCenterAdapter.java
     * 3. Package   :   com.ntels.onecable.view.mainmore
     * 4. Date      :   2016-12-01
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public ImageView rowBodyBullet;
        public TextView rowBodyTitle;
        public TextView rowBodyBody;
        public Button btnRowBodyOk;
        public TextView rowBodyDate;

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

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

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

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

        /**
         * 생성자.
         *
         * @param itemView Footer layout view
         */
        public FooterViewHolder(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();
            }
        }
    }

}
