package de.sebse.fuplanner.fragments.moddetails;

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

import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.KVV.types.Assignment;
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 ModDetailAssignmentAdapter extends BaseExpandableListAdapter {

    private Modules.Module mModule = null;

    @Override
    public String getChild(int groupPosition, int childPosititon) {
        StringBuilder sb = new StringBuilder();
        sb.append(this.getGroup(groupPosition).getInstructions());
        sb.append("\n\n");
        for (String s : this.getGroup(groupPosition).getUrls())
        {
            sb.append(s);
            sb.append("\n\n");
        }
        return sb.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 Assignment getGroup(int groupPosition) {
        if (this.mModule != null && this.mModule.assignments != null)
            return this.mModule.assignments.get(groupPosition);
        else
            return null;
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Assignment assignment = 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(assignment.getTitle());
        if(assignment.isOpen())
            itemHolder.mSubLeft.setText(itemHolder.mView.getResources().getText(R.string.open));
        else
            itemHolder.mSubLeft.setText(itemHolder.mView.getResources().getText(R.string.closed));
        itemHolder.mSubRight.setText(UtilsDate.getModifiedDateTime(parent.getContext(), assignment.getDueDate()));

        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();
    }
}