using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
 
 
namespace InitiateCall1
{
    public class Application
    {
        static void Main(string[] args)
        {
            UIApplication.Main(args);
        }
    }
 
 
    // The name AppDelegate is referenced in the MainWindow.xib file.
    public partial class AppDelegate : UIApplicationDelegate
    {
        // This method is invoked when the application has loaded its UI and its ready to run
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            // If you have defined a view, add it here:
            // window.AddSubview (navigationController.View);
            window.MakeKeyAndVisible();
            textfieldInput.AllEditingEvents += delegate
            {   //BUG: if this event is not wired up, the .Ended event doesn't fire... weird?
                //    Console.WriteLine("alleditingevents");
            };
            textfieldInput.Ended += delegate
            {
                Console.WriteLine("ended");
                textfieldInput.ResignFirstResponder();
            };
            buttonCall.TouchDown += delegate
            {
                InitiateAction("tel:", textfieldInput.Text);
            };
            buttonSms.TouchDown += delegate
            {
                InitiateAction("sms:", textfieldInput.Text);
            };
            buttonMap.TouchDown += delegate
            {
                NSUrl url = new NSUrl("http://maps.google.com/maps?q=" + textfieldInput.Text);
                if (!UIApplication.SharedApplication.OpenUrl(url))
                {
                    NotSupportedAlert("Google Maps");
                }
            };
            buttonWeb.TouchDown += delegate
            {
                InitiateAction("http://", textfieldInput.Text);
            };
            buttonMail.TouchDown += delegate
            {
                NSUrl url = new NSUrl("mailto:yourname@gmail.com?subject=" + textfieldInput.Text);
                if (!UIApplication.SharedApplication.OpenUrl(url))
                {
                    NotSupportedAlert("mailto:");
                }
            };
            buttonTweet.TouchDown += delegate
            {
                var number = textfieldInput.Text;
                NSUrl url = new NSUrl("tweetie:///post?message=" + number);
                if (!UIApplication.SharedApplication.OpenUrl(url))
                {
                    NotSupportedAlert("tweetie://");
                }
            };
            buttonGoogleEarth.TouchDown += delegate
            {
                InitiateAction("comgoogleearth://", textfieldInput.Text);
            };
            return true;
        }
        /// <summary>
        /// Call OpenURL for this scheme/parameter
        /// </summary>
        private void InitiateAction(string scheme, string parameter)
        {
            NSUrl url = new NSUrl(scheme + parameter);
            if (!UIApplication.SharedApplication.OpenUrl(url))
            {
                NotSupportedAlert(scheme);
            }
        }
        /// <summary>
        /// Show alert window if scheme isn't supported
        /// </summary>
        private void NotSupportedAlert(string scheme)
        {
            var av = new UIAlertView("Not supported"
                                    , "Scheme '" + scheme + "' is not supported on this device"
                                    , null
                                    , "Ok thanks"
                                    , null);
            av.Show();
        }
 
 
        // This method is required in iPhoneOS 3.0
        public override void OnActivated(UIApplication application)
        {
        }
    }
}