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

package com.ntels.onecable.view.homemember;

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.LogUtil;
import com.ntels.onecable.common.util.Util;

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

/**
 * <pre>
 * 1. ClassName :   FragmentHomeMemberAdapter
 * 2. FileName  :   FragmentHomeMemberAdapter.java
 * 3. Package   :   com.ntels.onecable.view.homemember
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class FragmentHomeMemberAdapter 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 Context context;
    private int listLayoutHeader;
    private int listLayoutItem;
    private int listLayoutFooter;
    private int type = 0;
    private Boolean isListCheck = false;
    private Boolean isPermissionCheck = false;
    private Boolean isDelCheck = false;
    private Boolean isHeaderCheck = false;
    private Boolean isFooterCheck = false;
    private Boolean isFooterVisible = false;

    /**
     * 생성자.
     *
     * @param context          컨텍스트
     * @param listLayoutHeader Header layout resource id
     * @param listLayoutItem   Item layout resource id
     * @param listLayoutFooter Footer layout resource id
     * @param alTmp            RecyclerView에 보여줄 data
     * @param adapterCallback  Callback listener
     */
    public FragmentHomeMemberAdapter(Context context, int listLayoutHeader, int listLayoutItem,
                                     int listLayoutFooter, ArrayList<HashMap<String, Object>> alTmp,
                                     ListInterface adapterCallback) {

        this.context = context;
        alTotalList = alTmp;
        this.listLayoutHeader = listLayoutHeader;
        this.listLayoutItem = listLayoutItem;
        this.listLayoutFooter = listLayoutFooter;

        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 #isListCheck} getter.
     *
     * @return true or false
     */
    public boolean getPermissionCheck() {

        return isPermissionCheck;
    }

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

        isPermissionCheck = isCheck;
    }
    /**
     * {@link #isListCheck} getter.
     *
     * @return true or false
     */
    public boolean getDelCheck() {

        return isDelCheck;
    }

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

        isDelCheck = isCheck;
    }

    /**
     * {@link #isHeaderCheck} getter.
     *
     * @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;
    }

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

        return isFooterVisible;
    }

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

        isFooterVisible = 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 ItemViewHoler(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 ItemViewHoler) {
                ItemViewHoler itemViewHolerView = (ItemViewHoler) holder;

                String memberTypeCode = util.null2string(itemMap.get(Constants.KEY_MEMBER_TYPE_CODE));
//                String invitation = util.null2string(itemMap.get(Constants.KEY_INVITATION));
                String nickname = util.null2string(itemMap.get(Constants.KEY_NICKNAME));
                String imageFileName = util.null2string(itemMap.get(Constants.KEY_IMAGE_FILE_NAME));
                String gradeCode = util.null2string(itemMap.get(Constants.KEY_GRADE_CODE));
                String memberId = util.null2string(itemMap.get(Constants.KEY_MEMBER_ID));
//                String checkYn = util.null2string(itemMap.get(Constants.KEY_CHECK_YN));

                util.setRefreshImage(context, imageFileName, itemViewHolerView.ivRowIcon);
                itemViewHolerView.tvRowMemberId.setText(memberId);

//                itemViewHolerView.ivRowBullet.setVisibility(View.GONE);
                if (FragmentHomeMember.HOMEGRP_MEMBER.equals(memberTypeCode)) {
//                    itemViewHolerView.ivRowBullet.setVisibility(View.GONE);
                    if ("".equals(nickname)) {
                        itemViewHolerView.tvRowTitle.setText(memberId);
                    } else {
                        itemViewHolerView.tvRowTitle.setText(nickname);
                    }
                    itemViewHolerView.tvRowBody.setSelected(true);
                    if (Constants.GRADE_CODE_MAS.equals(gradeCode)) {
//                        itemViewHolerView.ivRowBullet.setSelected(false);
                        itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_master_user));
                    } else {
//                        itemViewHolerView.ivRowBullet.setSelected(true);
                        itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_default_user));
                    }
                } else if (FragmentHomeMember.INVITATION_MEMBER.equals(memberTypeCode)) {
                    itemViewHolerView.tvRowTitle.setText(nickname);
                    itemViewHolerView.tvRowBody.setSelected(false);
                    itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_invitation_member));
                    /*switch (invitation) {
                        case "01":
                            itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_invitation_member));
                            break;
                        case "21":
                            itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_an_invitation_member));
                            break;
                        case "22":
                            itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_an_invitation_member_not_pass));
                            break;
                        case "23":
                            itemViewHolerView.tvRowBody.setText(util.getString(context, R.string.layout_an_invitation_member1));
                            break;
                        default:
                            break;
                    }*/
                }

                itemViewHolerView.ivPermission.setVisibility(View.GONE);
                itemViewHolerView.ivPermission.setSelected(false);
                itemViewHolerView.ivDel.setVisibility(View.GONE);
                itemViewHolerView.ivDel.setSelected(false);
                if (isPermissionCheck) {
                    LogUtil.v("jino", "isPermissionCheck = " + isPermissionCheck);
                    if (Constants.GRADE_CODE_SIM.equals(gradeCode)) {
                        itemViewHolerView.ivPermission.setVisibility(View.VISIBLE);
//                        if (checkYn.equals("Y")) {
//                            itemViewHolerView.ivPermission.setSelected(true);
//                        }
                    }
                } else if (isDelCheck) {
                    if (!Constants.GRADE_CODE_MAS.equals(gradeCode)) {
                        itemViewHolerView.ivDel.setVisibility(View.VISIBLE);
//                        if (checkYn.equals("Y")) {
//                            itemViewHolerView.ivDel.setSelected(true);
//                        }
                    }
                }
                itemViewHolerView.rlRowBody.setTag(position);
                itemViewHolerView.rlRowBody.setOnClickListener(new OnClickListener() {
                    int tag = 0;

                    @Override
                    public void onClick(View view) {
                        tag = (int) view.getTag();
                        if (isPermissionCheck) {
                            if (!Constants.GRADE_CODE_MAS.equals(alTotalList.get(tag).get(Constants.KEY_GRADE_CODE))) {
//                                if (alTotalList.get(tag).get(Constants.KEY_CHECK_YN).equals("Y")) {
//                                    alTotalList.get(tag).put(Constants.KEY_CHECK_YN, "N");
//                                } else {
//                                    alTotalList.get(tag).put(Constants.KEY_CHECK_YN, "Y");
//                                }
                                adapterCallback.onCallAction(3, tag);
                            }
                        } else if (isDelCheck) {
                            if (!Constants.GRADE_CODE_MAS.equals(alTotalList.get(tag).get(Constants.KEY_GRADE_CODE))) {
                                adapterCallback.onCallAction(2, tag);
                            }
                        }
                    }
                });
            } else if (holder instanceof FooterViewHolder) {

                FooterViewHolder vhItemView = (FooterViewHolder) holder;
                String moreYn = util.null2string(itemMap.get(Constants.KEY_MORE_YN));
                if (moreYn.equals("Y")) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    if (isListCheck || isPermissionCheck || isDelCheck) {
                        vhItemView.llFooterBody.setVisibility(View.GONE);
                    } else {
                        if (isFooterVisible) {
                            vhItemView.llFooterBody.setVisibility(View.VISIBLE);
                        } else {
                            vhItemView.llFooterBody.setVisibility(View.GONE);
                        }

                        vhItemView.llFooterBody.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {

                                adapterCallback.onCallAction(0, 0);
                            }
                        });
                    }
                }
            }
            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 :   ItemViewHoler
     * 2. FileName  :   FragmentHomeMemberAdapter.java
     * 3. Package   :   com.ntels.onecable.view.homemember
     * 4. Date      :   2016-12-01
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class ItemViewHoler extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public RelativeLayout rlRowBody;
        public ImageView ivRowIcon;
//        public ImageView ivRowBullet;
        public TextView tvRowTitle;
        public TextView tvRowMemberId;
        public TextView tvRowBody;
        public ImageView ivPermission;
        public ImageView ivDel;

        /**
         * 생성자.
         *
         * @param itemView Item layout view
         */
        public ItemViewHoler(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);
            tvRowMemberId = (TextView) itemView.findViewById(R.id.tvRowMemberId);
            tvRowBody = (TextView) itemView.findViewById(R.id.tvRowBody);
            ivPermission = (ImageView) itemView.findViewById(R.id.ivPermission);
            ivDel = (ImageView) itemView.findViewById(R.id.ivDel);
        }
    }

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

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public TextView tvTitle;

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

            super(itemView);

        }
    }

    /**
     * <pre>
     * 1. ClassName :   FooterViewHolder
     * 2. FileName  :   FragmentHomeMemberAdapter.java
     * 3. Package   :   com.ntels.onecable.view.homemember
     * 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 ImageView ivFooterAdd;

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