package com.example.scores.utilities;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

public class ScoreGenerator {
    // XML element and attribute names
    private static final String SCORES = "scores";
    private static final String SCORE = "score";
    private static final String TIME = "time";
    private static final String TEAM1 = "team1";
    private static final String TEAM2 = "team2";
    private static final String TEAM1_SCORE = "team1-score";
    private static final String TEAM2_SCORE = "team2-score";
    private static final String DESCRIPTION = "description";

    private XmlPullParser parser;
    private List<Score> scores;
    private int index = 0;

    public ScoreGenerator(XmlPullParser parser) {
        this.parser = parser;
        loadDataIntoList();
    }

    public Score getScore() {
        if ((scores != null) && (index < scores.size())) {
            return scores.get(index++);
        }
        else {
            return null;
        }
    }

    private void loadDataIntoList() {
        String team1 = null;
        String team2 = null;
        try {
            int eventType = parser.getEventType();
            Score currentScore = null;
            boolean done = false;
            while (eventType != XmlPullParser.END_DOCUMENT && !done) {
                String name = null;
                switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    scores = new ArrayList<Score>();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();

                    if (name.equalsIgnoreCase(SCORES)) {
                        team1 = parser.getAttributeValue(null, TEAM1);
                        team2 = parser.getAttributeValue(null, TEAM2);
                    }
                    else if (name.equalsIgnoreCase(SCORE)) {
                        currentScore = new Score(team1, team2);
                    }
                    else if (currentScore != null) {
                        if (name.equalsIgnoreCase(TIME)) {
                            currentScore.setTime(parser.nextText());
                        }
                        else if (name.equalsIgnoreCase(TEAM1_SCORE)) {
                            currentScore.setTeam1Score(Integer
                                    .parseInt(parser.nextText()));
                        }
                        else if (name.equalsIgnoreCase(TEAM2_SCORE)) {
                            currentScore.setTeam2Score(Integer
                                    .parseInt(parser.nextText()));
                        }
                        else if (name.equalsIgnoreCase(DESCRIPTION)) {
                            currentScore.setDescription(parser
                                    .nextText());
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase(SCORE) && currentScore != null) {
                        scores.add(currentScore);
                    }
                    else if (name.equalsIgnoreCase(SCORES)) {
                        done = true;
                    }
                    break;
                }
                eventType = parser.next();
            }
        }
        catch (Exception e) {
            System.err.println(e);
        }
    }
}
