/* Copyright (c) 2012 Sundog Software LLC. All rights reserved worldwide. */

// Boilerplate Android activity and UI code below for the most part... the SilverLining
// integration is mostly inside the SilverLiningDemoView.Renderer class actually.
// Note, however, how we pass the asset manager into the SilverLiningDemoView 
// when the activity is created - it needs this to know how to load SilverLining's
// resources, which you should include in your assets/Resources directory.

package com.sundogsoftware;

import com.sundogsoftware.silverlining.*;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TimePicker;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;


public class SilverLiningDemoActivity extends FragmentActivity {

    SilverLiningDemoView mView;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

		setContentView(R.layout.main);
		
		mView = (SilverLiningDemoView) findViewById(R.id.glSurface);
		// Give the view our asset manager so SilverLining can load its resources:
		mView.initialize(this.getAssets());
		
		addListeners();
    }

    @Override protected void onPause() {
        super.onPause();
        mView.onPause();
    }

    @Override protected void onResume() {
        super.onResume();
        mView.onResume();
    }
	
	public void showTimePickerDialog(View v) {
		DialogFragment newFragment = new TimePickerFragment(mView);
		newFragment.show(getSupportFragmentManager(), "timePicker");
	}

	// Handle UI components to toggle different cloud types and change the time of day...
	public void addListeners() {
		  
		CheckBox cumulusCheck = (CheckBox) findViewById(R.id.cumulus);
		cumulusCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( mView != null )
		        {
		            mView.enableCumulus(isChecked);
		        }
		    }
		});
		
		CheckBox cirrusCheck = (CheckBox) findViewById(R.id.cirrus);
		cirrusCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( mView != null )
		        {
		            mView.enableCirrus(isChecked);
		        }
		    }
		});
		
		CheckBox stratusCheck = (CheckBox) findViewById(R.id.stratus);
		stratusCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( mView != null )
		        {
		            mView.enableStratus(isChecked);
		        }
		    }
		});
	}
	
	public static class TimePickerFragment extends DialogFragment
								implements TimePickerDialog.OnTimeSetListener {
		
		protected SilverLiningDemoView mView;
		
		TimePickerFragment(SilverLiningDemoView view) {
			super();
			mView = view;
		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			if (mView == null) return null;
			
			// Use the current time as the default values for the picker
			LocalTime time = mView.getDateTime();

			// Create a new instance of TimePickerDialog and return it
			return new TimePickerDialog(getActivity(), this, time.GetHour(), time.GetMinutes(),
					DateFormat.is24HourFormat(getActivity()));
		}

		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mView.setTime(hourOfDay,  minute);
		}
	}
	
}
