package com.example.scores.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.example.scores.utilities.Score;
import com.example.scores.utilities.ScoreGenerator;

public class ScoresService3 extends Service {
    private ScoreGenerator scoreGenerator;

    public ScoresService3() {
    }

    public class ScoreBinder extends Binder {
        public void loadGame(int xmlResource) {
            scoreGenerator = new ScoreGenerator(getResources()
                    .getXml(xmlResource));
        }

        public void nextScore() {
            if (scoreGenerator != null) {
                Score score = scoreGenerator.getScore();
                broadcastScore(score.toString());
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new ScoreBinder();
    }

    private void broadcastScore(String text) {
        Intent intent = new Intent("com.example.scores.BROADCAST");
        intent.putExtra("Text", text);
        getApplicationContext().sendBroadcast(intent);
    }
}