package com.example.scores.activities;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.scores.R;
import com.example.scores.services.ScoresService3;

public class ScoresActivity3 extends Activity {
    private boolean isBound;
    private ScoresService3.ScoreBinder binder;
    private ServiceConnection conn = new ScoreServiceConnection();

    class ScoreServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName c, IBinder svc) {
            binder = (ScoresService3.ScoreBinder) svc;
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName c) {
            isBound = false;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(ScoresActivity3.this,
                ScoresService3.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isBound) {
            unbindService(conn);
            isBound = false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores3);

        final RadioGroup rg = (RadioGroup) findViewById(R.id.game);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup grp, int chkId) {
                if (chkId == R.id.rb1) {
                    binder.loadGame(R.xml.phxden);
                }
                else {
                    binder.loadGame(R.xml.boschi);
                }
            }
        });

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                if (isBound) {
                    binder.nextScore();
                }
            }
        });

    }
}