package com.hikapro.backpack.presenter.adapters;

import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hikapro.backpack.App;
import com.hikapro.backpack.R;
import com.hikapro.backpack.presenter.SetListPresenter;
import com.hikapro.backpack.presenter.adapters.helper.sets.ItemTouchHelperAdapter;
import com.hikapro.backpack.view.recycler.SetViewHolder;

/**
 * Created by tariel on 20/04/16.
 */
public class SetListAdapter extends  RecyclerView.Adapter<SetViewHolder> implements ItemTouchHelperAdapter{

    private SetListPresenter presenter;
    private String rightBracket;

    public SetListAdapter(SetListPresenter presenter) {
        this.presenter = presenter;
    }

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

        final com.hikapro.backpack.model.entities.Set set = presenter.getModel().getSetByPosition(position);

        if (rightBracket == null || rightBracket.isEmpty()) {

            if (presenter.getActivityContext() != null)
                this.rightBracket = presenter.getActivityContext().getResources().
                        getString(R.string.right_angle_bracket);
            else
                this.rightBracket = "";
        }
        holder.textView.setText(set.getName() + " " + rightBracket);
        holder.textView.setTypeface(App.getMainFace());
        String info = String.format("%s %d / %d",
                presenter.getActivityContext().getResources().getString(R.string.packed),
                set.getPackedQty(),
                set.getActiveQty());
        holder.setInfo.setText(info);
        holder.cardView.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                presenter.showItemList(set);
            }
        });
        holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                presenter.onLongClick(holder);
                return false;
            }
        });
        holder.applyBackground(presenter.getAppContext(), set.getId());


    }

    @Override
    public SetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        SetViewHolder viewHolder;
        android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.set_card,
                parent, false);
        viewHolder = new SetViewHolder(v);
        return viewHolder;
    }

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

    //GLM_add_resources_SetList
    @Override
    public void onItemDismiss(int position) {
        presenter.onItemDismiss(position);
    }

    //GLM_add_resources_SetList
    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        return presenter.onItemMove(fromPosition, toPosition);
    }
}
