package de.sebse.fuplanner.fragments.moddetails;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import java.util.ArrayList;

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

class ModDetailAnnounceAdapter extends BaseExpandableListAdapter {

    private Modules.Module mModule = null;

    @Override
    public String getChild(int groupPosition, int childPosititon) {
        StringBuilder s = new StringBuilder(this.getGroup(groupPosition).getBody());
        ArrayList<String> urls = this.getGroup(groupPosition).getUrls();
        for (int j =0; j<urls.size(); j++){
            s.append("\n");
            s.append(urls.get(j));
        }
        return s.toString();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = getChild(groupPosition, childPosition);

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_all_string, parent, false);
        }

        StringViewHolder itemHolder = new StringViewHolder(convertView);
        itemHolder.mString.setText(childText);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Announcement getGroup(int groupPosition) {
        if (this.mModule != null && this.mModule.announcements != null)
            return this.mModule.announcements.get(groupPosition);
        else
            return null;
    }

    @Override
    public int getGroupCount() {
        if (this.mModule != null && this.mModule.announcements != null)
            return this.mModule.announcements.size();
        else
            return 0;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Announcement announce = getGroup(groupPosition);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_all_items, parent, false);
        }

        ItemViewHolder itemHolder = new ItemViewHolder(convertView);
        itemHolder.mTitle.setText(announce.getTitle());
        itemHolder.mSubLeft.setText(announce.getCreatedBy());
        itemHolder.mSubRight.setText(UtilsDate.getModifiedDateTime(parent.getContext(), announce.getCreatedOn()));

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

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

    public void setModule() {
        this.notifyDataSetChanged();
    }
}