package de.sebse.fuplanner.fragments.moddetails;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.KVV.types.Event;
import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.tools.UtilsDate;
import de.sebse.fuplanner.tools.logging.Logger;
import de.sebse.fuplanner.tools.ui.ItemViewHolder;
import de.sebse.fuplanner.tools.ui.StringViewHolder;

class ModDetailEventAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private static final int SECTION_UPCOMING = 0;
    private static final int SECTION_PAST = 1;

    private Modules.Module mValue;
    private final ArrayList<Pair<Integer, Integer>> mPositionalData;

    private final Logger log = new Logger(this);

    ModDetailEventAdapter() {
        mValue = null;
        mPositionalData = new ArrayList<>();
        //mListener = listener;
    }

    public void setModule(Modules.Module module) {
        mValue = module;
        this.setModule();
    }

    public void setModule() {
        mPositionalData.clear();
        addPositionalListData(getUpcomingEventsCount(), SECTION_UPCOMING);
        mPositionalData.add(new Pair<>(TYPE_HEADER, SECTION_PAST));
        addPositionalListData(getPastEventsCount(), SECTION_PAST);

        this.notifyDataSetChanged();
    }

    private void addPositionalListData(int count, int category) {
        for (int i = 0; i < count; i++) {
            mPositionalData.add(new Pair<>(TYPE_ITEM, category+1024*i));
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_HEADER:
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_all_caption, parent, false);
                return new StringViewHolder(view);
            case TYPE_ITEM:
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_all_items, parent, false);
                return new ItemViewHolder(view);
            default:
                //noinspection ConstantConditions
                return null;
        }
    }

    @Override
    public int getItemViewType(int position) {
        // Note that unlike in ListView adapters, types don't have to be contiguous
        if (position < mPositionalData.size())
            return mPositionalData.get(position).first;
        else return -1;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mValue == null || position > mPositionalData.size())
            return;
        Pair<Integer, Integer> data = mPositionalData.get(position);
        switch (data.first) {
            case TYPE_HEADER:
                StringViewHolder h = (StringViewHolder) holder;
                switch (data.second) {
                    case SECTION_PAST:
                        h.mString.setText(R.string.past_events);
                        break;
                    case SECTION_UPCOMING:
                        h.mString.setText(R.string.upcoming_events);
                        break;
                }
                break;
            case TYPE_ITEM:
                int section = data.second % 1024;
                int index = data.second / 1024;
                ItemViewHolder i = (ItemViewHolder) holder;
                Event event = null;
                switch (section) {
                    case SECTION_UPCOMING:
                        event = mValue.events.getUpcoming(index);
                        break;
                    case SECTION_PAST:
                        event = mValue.events.getPast(index);
                        break;
                }
                //noinspection ConstantConditions
                i.mTitle.setText(event.getTitle());
                i.mSubLeft.setText(event.getType());
                String start, end;
                if (UtilsDate.dateEquals(event.getStartDate(), System.currentTimeMillis()))
                    start = UtilsDate.getModifiedTime(i.mView.getContext(), event.getStartDate());
                else
                    start = UtilsDate.getModifiedDateTime(i.mView.getContext(), event.getStartDate());
                if (UtilsDate.dateEquals(event.getStartDate(), event.getEndDate()))
                    end = UtilsDate.getModifiedTime(i.mView.getContext(), event.getEndDate());
                else
                    end = UtilsDate.getModifiedDateTime(i.mView.getContext(), event.getEndDate());
                i.mSubRight.setText(i.mView.getResources().getString(R.string.date_scale,
                        start, end
                ));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mPositionalData.size();
    }

    private int getUpcomingEventsCount() {
        if (mValue.events != null)
            return mValue.events.sizeUpcoming();
        return 0;
    }

    private int getPastEventsCount() {
        if (mValue.events != null)
            return mValue.events.sizePast();
        return 0;
    }
}
