package com.hikapro.backpack.presenter.adapters;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.Toast;

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.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 12/05/16.
 */
public class PackedListAdapter extends ItemListAdapter {

    public PackedListAdapter(Presenter.ItemList presenter) {
        super(presenter);
    }

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

        final Item item = presenter.getModel().getItemByPosition(position);
        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;
        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:
                        unpack(item);
                        break;
                    case ID_ADD_TO_BAG:
                        pack(item);
                        break;
                }
                menuLayout.notifyOnMenuItemClick();
            }
        };

        menuLayout.addMenu(menu, onClickListener);
    }

    @Override
    public void remove(Item item) {
        presenter.getModel().unpackItem(item);
        presenter.getModel().remove(item);
         presenter.getModel().pendingRemove(item);
    }

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

    @Override
    public void pack(Item item) {
        item.setPacked(false);
        super.pack(item);
    }
}
