package com.hikapro.backpack.presenter.adapters;

import android.content.res.Resources;
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.CheckBox;
import android.widget.LinearLayout;

import com.hikapro.backpack.R;
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.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.ItemViewHolder;

/**
 * Created by tariel on 23/04/16.
 */
public class ItemDetailAdapter extends RecyclerView.Adapter<ItemViewHolder> {

    private Presenter.ItemDetail presenter;

    public ItemDetailAdapter(Presenter.ItemDetail presenter) {

        this.presenter = presenter;
    }

    @Override
    public int getItemCount() {
        return presenter.getModel().getCount();
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {
        final Item item = presenter.getModel().getCurrentItem();
        holder.cb_item.setChecked(item.isPacked());
        holder.tv_text.setText(item.getName());
        holder.im_info.setVisibility(View.GONE);
        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);
                }
            }
        });


        final SwipeMenuLayout menuLayout = (SwipeMenuLayout)holder.menu;
        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(ItemListAdapter.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(ItemListAdapter.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(ItemListAdapter.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);
        }

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

    @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 leave(Item item) {
        presenter.getModel().pendingRemove(item);
    }

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

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

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

}
