package com.hikapro.backpack.presenter.adapters;

import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hikapro.backpack.App;
import com.hikapro.backpack.R;
import com.hikapro.backpack.model.entities.Category;
import com.hikapro.backpack.model.entities.Item;
import com.hikapro.backpack.presenter.Presenter;
import com.hikapro.backpack.presenter.adapters.helper.Util;
import com.hikapro.backpack.presenter.adapters.helper.items.StickyHeaderAdapter;
import com.hikapro.backpack.presenter.adapters.helper.items.swipe2.SwipableElement;
import com.hikapro.backpack.presenter.adapters.helper.items.swipe2.SwipeMenu;
import com.hikapro.backpack.presenter.adapters.helper.items.swipe2.SwipeMenuItem;
import com.hikapro.backpack.presenter.adapters.helper.items.swipe2.SwipeMenuLayout;
import com.hikapro.backpack.view.recycler.HeaderViewHolder;
import com.hikapro.backpack.view.recycler.ItemViewHolder;

/**
 * Created by tariel on 01/05/16.
 */
public class ItemListAdapter extends RecyclerView.Adapter<ItemViewHolder> implements StickyHeaderAdapter<HeaderViewHolder> {

    protected static final int ID_DELETE = 1;
    protected static final int ID_LEAVE_AT_HOME = 2;
    protected static final int ID_ADD_TO_BAG = 3;
    protected static final int ID_RETURN_TO_LIST = 4;

    protected Presenter.ItemList presenter;
    protected boolean checkAll;
    protected int addToCount = 0;

    public ItemListAdapter(Presenter.ItemList presenter) {
        this.presenter = presenter;
    }

    @Override
    public int getItemCount() {
        int ret = presenter.getModel().getItemsCount();
        return ret + addToCount;
    }

    @Override
    public long getItemId(int position) {
        long ret =  presenter.getModel().getItemId(position);
        return ret;
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {

        try {
            final Item item = presenter.getModel().getItemByPosition(position);
            if (item != null) {
                setVisibility(holder, View.VISIBLE);
                holder.cb_item.setChecked(checkAll);
                holder.tv_text.setText(item.getName());
                if (item.isPendingRemoval()) {
                    holder.tv_text.setPaintFlags(holder.tv_text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    holder.cb_item.setVisibility(View.GONE);
                } else {
                    //holder.cb_item.setVisibility(View.VISIBLE);
                    holder.tv_text.setPaintFlags(holder.tv_text.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                }

                holder.cb_item.setOnClickListener(new android.view.View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (holder.cb_item.isChecked()) {
                            pack(item);
                        } else {
                            unpack(item);
                        }
                    }
                });
                holder.im_info.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        presenter.showDetails(position);
                    }
                });

                final SwipeMenuLayout menuLayout = (SwipeMenuLayout) holder.menu;
                final SwipeMenu menu = new SwipeMenu(presenter.getActivityContext());

                menuLayout.removeAllViews();

                // create menu items here
                if (!item.isPendingRemoval()) {
                    // create "leave at home" item
                    SwipeMenuItem leaveAtHomeItem = new SwipeMenuItem(menu.getContext());
                    // set id
                    leaveAtHomeItem.setId(ID_LEAVE_AT_HOME);
                    // set item background
                    leaveAtHomeItem.setBackground(R.color.colorLeaveAtHomeBtnBackground);
                    // set item width
                    leaveAtHomeItem.setWidth(Util.dp2px(presenter.getActivityContext(), 120));
                    // set item title
                    leaveAtHomeItem.setTitle(R.string.leave_at_home_button);
                    // set item title font size
                    leaveAtHomeItem.setTitleSize(14);
                    // set item title font color
                    leaveAtHomeItem.setTitleColor(Color.WHITE);
                    // add to menu
                    menu.addMenuItem(leaveAtHomeItem);
                } else {
                    // create "add to bag" item
                    SwipeMenuItem addToBagItem = new SwipeMenuItem(menu.getContext());
                    // set id
                    addToBagItem.setId(ID_ADD_TO_BAG);
                    // set item background
                    addToBagItem.setBackground(R.color.colorAddToBagBtnBackground);
                    // set item width
                    addToBagItem.setWidth(Util.dp2px(presenter.getActivityContext(), 120));
                    // set item title
                    addToBagItem.setTitle(R.string.add_to_bag_button);
                    // set item title font size
                    addToBagItem.setTitleSize(14);
                    // set item title font color
                    addToBagItem.setTitleColor(Color.WHITE);
                    // add to menu
                    menu.addMenuItem(addToBagItem);

                    // create "return to list" item
                    SwipeMenuItem returnToListItem = new SwipeMenuItem(menu.getContext());
                    // set id
                    returnToListItem.setId(ID_RETURN_TO_LIST);
                    // set item background
                    returnToListItem.setBackground(R.color.colorReturnToListBtnBackground);
                    // set item width
                    returnToListItem.setWidth(Util.dp2px(presenter.getActivityContext(), 120));
                    // set item title
                    returnToListItem.setTitle(R.string.return_to_list_button);
                    // set item title font size
                    returnToListItem.setTitleSize(14);
                    // set item title font color
                    returnToListItem.setTitleColor(Color.WHITE);
                    // add to menu
                    menu.addMenuItem(returnToListItem);
                }

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(menu.getContext());
                // set id
                deleteItem.setId(ID_DELETE);
                // set item background
                deleteItem.setBackground(R.color.colorDeleteBtnBackground);
                // set item width
                deleteItem.setWidth(Util.dp2px(presenter.getActivityContext(), 120));
                // set item title
                deleteItem.setTitle(R.string.delete_button);
                // set item title font size
                deleteItem.setTitleSize(14);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(deleteItem);

                View.OnClickListener onClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case ID_DELETE:
                                remove(item);
                                break;
                            case ID_LEAVE_AT_HOME:
                                leave(item);
                                break;
                            case ID_RETURN_TO_LIST:
                                leaveCancel(item);
                                break;
                            case ID_ADD_TO_BAG:
                                pack(item);
                                break;
                        }
                        menuLayout.notifyOnMenuItemClick();

                    }
                };

                menuLayout.addMenu(menu, onClickListener);
            } else {
                if (presenter.getModel().getPackedQty() > 0) {
                    setVisibility(holder, View.GONE);
                }
            }

        } catch (Exception ex) {

        }
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder viewHolder;
        View content = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_element, parent, false);
        SwipeMenuLayout menu = new SwipeMenuLayout(parent.getContext());
        menu.setId(R.id.menu);
        SwipableElement container = new SwipableElement(parent.getContext(), content, menu);
        container.setSwipeDirection(SwipableElement.DIRECTION_LEFT);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Util.dp2px(parent.getContext(),66));
        container.setOrientation(LinearLayout.HORIZONTAL);
        container.setLayoutParams(params);
        viewHolder = new ItemViewHolder(container);
        return viewHolder;
    }

    public void filter(String query) {
        presenter.getModel().filter(query);
        notifyDataSetChanged();
    }

    @Override
    public long getHeaderId(int position) {
        /*if (position == 0) {
            return -1;
        } else {*/
            return presenter.getModel().getHeaderId(position);
        //}
    }

    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        HeaderViewHolder viewHolder;
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.header_element, parent, false);
        viewHolder = new HeaderViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindHeaderViewHolder(HeaderViewHolder holder, int position) {
        Category category = presenter.getModel().getCategoryByPosition(position);
        holder.id = category.getId();
        holder.title.setText(category.getName());
        holder.title.setTypeface(App.getMainFace());
        holder.title.setBackgroundColor(0x2B1E15);
    }

    public void leave(Item item) {
        presenter.getModel().pendingRemove(item);
    }

    public void leaveCancel(Item item) {
        presenter.getModel().pendingRemoveCancel(item);
    }

    public void remove(Item item) {
        presenter.getModel().remove(item);
    }

    public void pack(Item item) {
        if (!item.isPacked())
            presenter.getModel().packItem(item);
    }

    public void unpack(Item item) {
        if (item.isPacked())
            presenter.getModel().unpackItem(item);
    }

    public void setCheckAll(boolean checkAll) {
        this.checkAll = checkAll;
    }

    public void addEmptyToTheEnd() {
        addToCount = 1;
        notifyDataSetChanged();
    }
    public void removeEmptyFromTheEnd() {
        addToCount = 0;
        notifyDataSetChanged();
    }

    private void setVisibility(ItemViewHolder holder, int visibility) {
        if (holder != null) {
            holder.cb_item.setVisibility(visibility);
            holder.im_info.setVisibility(visibility);
            holder.menu.setVisibility(visibility);
            holder.tv_text.setVisibility(visibility);
        }
    }
}
