package com.arqui.tomasulo.fragments;

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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.arqui.tomasulo.R;
import com.arqui.tomasulo.TomasuloContext;
import com.arqui.tomasulo.listeners.IClockListener;
import com.arqui.tomasulo.listeners.ISimulationListener;

public class ControlsFragment extends Fragment {

	private List<ISimulationListener> mSimulationListeners;
	private IClockListener mClockListener;
	private TomasuloContext mContext;

	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		mSimulationListeners = new ArrayList<ISimulationListener>();
		mContext = (TomasuloContext) activity.getApplicationContext();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {		
		View rootView = inflater.inflate(R.layout.fragment_controls, container, false);

		rootView.findViewById(R.id.btn_pause).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mContext.mClockCycleCount = 0;
				mContext.mSimluationStarted = false;
				updateUI(getView());
				for(ISimulationListener listener : mSimulationListeners)
					listener.onSimulationEnded();
			}
		});

		rootView.findViewById(R.id.btn_play).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mContext.mClockCycleCount = 0;
				mContext.mSimluationStarted = true;
				updateUI(getView());
				for(ISimulationListener listener : mSimulationListeners)
					listener.onSimulationStarted();
			}
		});

		rootView.findViewById(R.id.btn_step).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(mContext.mSimluationStarted)
				{
					mContext.mClockCycleCount++;
					updateUI(getView());

					if(mClockListener != null)
						mClockListener.onClock();
				}
			}
		});

		updateUI(rootView);
		return rootView;
	}

	private void updateUI(View rootView)
	{
		ImageView btnPlay = (ImageView) rootView.findViewById(R.id.btn_play);
		ImageView btnPause = (ImageView) rootView.findViewById(R.id.btn_pause);
				
		if(mContext.mSimluationStarted)
		{
			btnPlay.setBackgroundColor(getResources().getColor(R.color.table_row_header));
			btnPause.setBackgroundColor(getResources().getColor(android.R.color.transparent));
		}else
		{
			btnPause.setBackgroundColor(getResources().getColor(R.color.table_row_header));
			btnPlay.setBackgroundColor(getResources().getColor(android.R.color.transparent));
		}
		
		TextView tvClockCycleCount = (TextView) rootView.findViewById(R.id.clock_cycle_count);
		tvClockCycleCount.setText(String.valueOf(mContext.mClockCycleCount));
	}
	
	public void addSimulationListener(ISimulationListener pSimulationListener) {
		if(!mSimulationListeners.contains(pSimulationListener)) {
			mSimulationListeners.add(pSimulationListener);
		}
	}

	public IClockListener getClockListener() {
		return mClockListener;
	}

	public void setClockListener(IClockListener mClockListener) {
		this.mClockListener = mClockListener;
	}
}
