/*<Summary>
         * Just our own converter for the  alignment enumerations
         * </Summery>*/
        private VerticalAlignment ConvertVerticalAlignment(VERTICAL_ALIGNMENT f)
        {
            switch (f)
            {
            case VERTICAL_ALIGNMENT.BOTTOM:
                return(VerticalAlignment.Bottom);

            case VERTICAL_ALIGNMENT.TOP:
                return(VerticalAlignment.Top);

            case VERTICAL_ALIGNMENT.CENTER:
                return(VerticalAlignment.Center);

            default:
                return(VerticalAlignment.Center);
            }
        }
        /// <summary>
        /// Used to create an AdMob ad
        /// Pass in your ad mob unit id, the format, height/width, position and whether its a test ad or not
        /// The testAd bool is important, it allows us to test an admob ad
        /// </summary>
        public void CreateAd(string adUnit, AD_FORMATS format, HORIZONTAL_ALIGNMENT hAlign, VERTICAL_ALIGNMENT vAlign, bool testAd)
        {
#if WINDOWS_PHONE
            if (Dispatcher.InvokeOnUIThread == null && Messenger != null)
            {
                Messenger("Dispatcher Error: UI Thread not set up");
                return;
            }

            Dispatcher.InvokeOnUIThread(() =>
            {
                BuildAd(adUnit, format, hAlign, vAlign, testAd);
            });
#endif
        }
        private void BuildAd(string adUnit, AD_FORMATS format, HORIZONTAL_ALIGNMENT hAlign, VERTICAL_ALIGNMENT vAlign, bool testAd)
        {
#if WINDOWS_PHONE
            //Check if an AdMob ad has already been created
            //If so we return, and display a message to the user
            if (_AdMobAd != null)
            {
                if (Messenger != null)
                {
                    Messenger("AdMob Ad already present");
                }
                return;
            }

            //Next we check if we have a grid
            if (_baseGrid == null)
            {
                _isAMAd = false;
                if (Messenger != null)
                {
                    Messenger("Missing grid");
                }
                return;
            }

            try
            {
                _AdMobAd = new AdView
                {
                    Format   = ConvertAdFormat(format),
                    AdUnitID = adUnit
                };

                if (format != AD_FORMATS.SMART_BANNER)
                {
                }

                _AdMobAd.VerticalAlignment = ConvertVerticalAlignment(vAlign);

                _AdMobAd.ReceivedAd        += OnAdReceived;
                _AdMobAd.FailedToReceiveAd += OnError;


                _baseGrid.Children.Add(_AdMobAd);
                //if (_baseGrid.Children.Count > 0)
                //    _adIndex = _baseGrid.Children.Count - 1;

                if (testAd)
                {
                    AdRequest adRequest = new AdRequest();
                    adRequest.ForceTesting = true;
                    _AdMobAd.LoadAd(adRequest);
                }


                //Finally add it to our grid
                _baseGrid.Children.Add(_AdMobAd);

                _isAMAd = true;
            }
            catch (Exception e)
            {
                if (Messenger != null)
                {
                    Messenger("AdMob Error: " + e.Message);
                }
                _isAMAd = false;
            }
#endif
        }
        /// <summary>
        /// Used to create a pubcenter ad
        /// Pass in your apId, unitId, height/width, position and whether its a test ad or not
        /// The testAd bool is important, it allows us to test an admob ad
        /// </summary>
        public void CreateAd(string apID, string unitId, double height, double width, bool autoRefresh, HORIZONTAL_ALIGNMENT hAlign, VERTICAL_ALIGNMENT vAlign)
        {
#if WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE
            if (Dispatcher.InvokeOnUIThread == null)
            {
                return;
            }

            Dispatcher.InvokeOnUIThread(() =>
            {
                BuildAd(apID, unitId, height, width, autoRefresh, hAlign, vAlign);
            });
#endif
        }
        /// <summary>
        /// Actually goes and does the hard work of building the Pubcenter Ad
        /// </summary>
        private void BuildAd(string apID, string unitId, double height, double width, bool autoRefresh, HORIZONTAL_ALIGNMENT hAlign, VERTICAL_ALIGNMENT vAlign)
        {
#if WINDOWS_PHONE_APP || WINDOWS_APP || WINDOWS_PHONE
            //if the ad already exists we return
            if (_Ad != null)
            {
                if (Messenger != null)
                {
                    Messenger("PubCenter Ad already created");
                }
                return;
            }

            if (BaseGrid == null)
            {
                _isPCAd = false;
                if (Messenger != null)
                {
                    Messenger("Missing grid");
                }
                return;
            }

            try
            {
                //Create the AdControl object
                _Ad = new AdControl();
                _Ad.ApplicationId        = apID;                              //Set the ap id
                _Ad.AdUnitId             = unitId;                            //Set the unit id
                _Ad.IsAutoRefreshEnabled = autoRefresh;                       //Set the autorefresh property

                _Ad.Width  = width;                                           //Set its width
                _Ad.Height = height;                                          //Set its height

                _Ad.VerticalAlignment   = ConvertVerticalAlignment(vAlign);   //Set its vertical position
                _Ad.HorizontalAlignment = ConvertHorizontalAlignment(hAlign); //Set its horizontal position


                //On error and on refreshed
                _Ad.ErrorOccurred += OnError;
                _Ad.AdRefreshed   += OnRefreshed;

                _isPCAd = true;


                //Finally add it to our grid
                _baseGrid.Children.Add(_Ad);

                if (Messenger != null)
                {
                    Messenger("PubCenter Ad Successfully Created");
                }
            }
            catch (Exception e)
            {
                if (Messenger != null)
                {
                    Messenger("Pubcenter Error: " + e.Message);
                }
                _isPCAd = false;
            }
#endif
        }
        /*<Summary>
         * AdMob overload
         * </Summery>*/
        public void CreateAd(string adUnit, AD_FORMATS format, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign, double width, double height, bool testAd)
        {
#if WINDOWS_PHONE
            Dispatcher.InvokeOnUIThread(() =>
            {
                BuildAd(adUnit, format, horizontalAlign, vertAlign, width, height, testAd);
            });
#endif
        }
        /*<Summary>
         * Builds an admob ad
         * </Summery>*/
        private void BuildAd(string adUnit, AD_FORMATS format, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign, double width, double height, bool testAd)
        {
#if WINDOWS_PHONE
            if (_ad_Google != null)
            {
                return;
            }

            if (!HasGrid())
            {
                _isBuilt      = false;
                _errorMessage = "Missing grid to attach ad to";
            }

            try
            {
                _ad_Google = new AdView
                {
                    Format   = ConvertAdFormat(format),
                    AdUnitID = adUnit
                };

                if (format != AD_FORMATS.SMART_BANNER)
                {
                    _ad_Google.Width  = width;
                    _ad_Google.Height = height;
                    _ad_Google.HorizontalAlignment = ConvertHorizontalAlignment(horizontalAlign);
                }

                _ad_Google.VerticalAlignment = ConvertVerticalAlignment(vertAlign);

                _ad_Google.ReceivedAd        += OnAdReceived;
                _ad_Google.FailedToReceiveAd += OnFailedToReceiveAd;


                baseGrid.Children.Add(_ad_Google);
                if (baseGrid.Children.Count > 0)
                {
                    _adIndex = baseGrid.Children.Count - 1;
                }

                if (testAd)
                {
                    AdRequest adRequest = new AdRequest();
                    adRequest.ForceTesting = true;
                    _ad_Google.LoadAd(adRequest);
                }

                _isBuilt = true;
            }
            catch (Exception e)
            {
                _errorMessage = "AdMob Error: " + e.Message;
                _isBuilt      = false;
            }
#endif
        }
        //This function is public and called from Unity
        //It then uses the dispatcher to invoke on the UI thread

        /*<Summary>
         * Windows ad service overload
         * </Summery>*/
        public void CreateAd(string apId, string unitId, double height, double width, bool autoRefresh, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign)
        {
#if WINDOWS_PHONE || NETFX_CORE
            Dispatcher.InvokeOnUIThread(() =>
            {
                BuildAd(apId, unitId, height, width, autoRefresh, horizontalAlign, vertAlign);
            });
#endif
        }
        /*<Summary>
         * Builds a windows ad service ad
         * </Summery>*/
        private void BuildAd(string apId, string unitId, double height, double width, bool autoRefresh, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign)
        {
#if WINDOWS_PHONE || NETFX_CORE
            //Phone implementation
            //If the basegrid object is null it will just return, just a safety check
            //Creates the adControl object
            //Sets its height, width, appid, unitid and such and then uses the margin to position it on screen
            //Finally its added to the children of the basegrid

            //A check to make sure we do not get duplicate ads
            if (_ad != null)
            {
                return;
            }

            //This should be called prior to calling this function but just in case...
            //Lets check if the grid/panel exists
            if (!HasGrid())
            {
                _isBuilt      = false;
                _errorMessage = "Missing grid to attach ad to";
            }
            try
            {
                _ad = new AdControl();
                _ad.ApplicationId        = apId;
                _ad.AdUnitId             = unitId;
                _ad.IsAutoRefreshEnabled = autoRefresh;

                _ad.Width  = width;
                _ad.Height = height;

                _ad.VerticalAlignment   = ConvertVerticalAlignment(vertAlign);
                _ad.HorizontalAlignment = ConvertHorizontalAlignment(horizontalAlign);


#if WINDOWS_PHONE
                baseGrid.Children.Add(_ad);

                if (baseGrid.Children.Count > 0)
                {
                    _adIndex = baseGrid.Children.Count - 1;
                }
#elif NETFX_CORE
                backPanel.Children.Add(_ad);

                if (backPanel.Children.Count > 0)
                {
                    _adIndex = backPanel.Children.Count - 1;
                }
#endif
                _ad.ErrorOccurred += OnAdError_ErrorOccurred;

                _ad.AdRefreshed += OnAdRefreshed;

                _isBuilt = true;
                return;
            }
            catch (Exception e)
            {
                _errorMessage = "PubCenter Error: " + e.Message;
                _isBuilt      = false;
            }
#else
            //function will return and do nothing if its in Unity editor
            _isBuilt = false;
#endif
        }
 /*<Summary>
  * Just our own converter for the  alignment enumerations
 </Summery>*/
 private VerticalAlignment ConvertVerticalAlignment(VERTICAL_ALIGNMENT f)
 {
     switch (f)
     {
         case VERTICAL_ALIGNMENT.BOTTOM:
             return VerticalAlignment.Bottom;
         case VERTICAL_ALIGNMENT.TOP:
             return VerticalAlignment.Top;
         case VERTICAL_ALIGNMENT.CENTER:
             return VerticalAlignment.Center;
         default:
             return VerticalAlignment.Center;
     }
 }
        /*<Summary>
         * Builds an admob ad
        </Summery>*/
        private void BuildAd(string adUnit, AD_FORMATS format, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign, double width, double height, bool testAd)
        {
#if WINDOWS_PHONE
           if(_ad_Google != null)
            return;

           if (!HasGrid())
           {
               _isBuilt = false;
               _errorMessage = "Missing grid to attach ad to";
           }

            try
            {
                _ad_Google = new AdView
                {
                    Format = ConvertAdFormat(format),
                    AdUnitID = adUnit
                };
                
                if (format != AD_FORMATS.SMART_BANNER)
                {
                    _ad_Google.Width = width;
                    _ad_Google.Height = height;
                    _ad_Google.HorizontalAlignment = ConvertHorizontalAlignment(horizontalAlign);
                }
   
                _ad_Google.VerticalAlignment = ConvertVerticalAlignment(vertAlign);
            
                _ad_Google.ReceivedAd += OnAdReceived;
                _ad_Google.FailedToReceiveAd += OnFailedToReceiveAd;


                baseGrid.Children.Add(_ad_Google);
                if (baseGrid.Children.Count > 0)
                    _adIndex = baseGrid.Children.Count - 1;

                if (testAd)
                {
                    AdRequest adRequest = new AdRequest();
                    adRequest.ForceTesting = true;
                    _ad_Google.LoadAd(adRequest);
                }
                
                _isBuilt = true;
            }
            catch(Exception e)
            {
                _errorMessage = "AdMob Error: " + e.Message;
                _isBuilt = false;
            }
#endif
        }
        /*<Summary>
         * AdMob overload
        </Summery>*/
        public void CreateAd(string adUnit, AD_FORMATS format, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign, double width, double height, bool testAd)
        {
#if WINDOWS_PHONE
            Dispatcher.InvokeOnUIThread(() =>
            {
                BuildAd(adUnit, format,horizontalAlign,vertAlign,width,height,testAd);
            });
#endif
        }
        /*<Summary>
         * Builds a windows ad service ad
        </Summery>*/
        private void BuildAd(string apId, string unitId, double height, double width, bool autoRefresh, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign)
        {
#if WINDOWS_PHONE || NETFX_CORE
           //Phone implementation
           //If the basegrid object is null it will just return, just a safety check
           //Creates the adControl object
           //Sets its height, width, appid, unitid and such and then uses the margin to position it on screen
           //Finally its added to the children of the basegrid

           //A check to make sure we do not get duplicate ads
           if(_ad != null)
            return;

           //This should be called prior to calling this function but just in case...
           //Lets check if the grid/panel exists
           if (!HasGrid())
           {
               _isBuilt = false;
               _errorMessage = "Missing grid to attach ad to";
           }
           try
           {
               _ad = new AdControl();
               _ad.ApplicationId = apId;
               _ad.AdUnitId = unitId;
               _ad.IsAutoRefreshEnabled = autoRefresh;

               _ad.Width = width;
               _ad.Height = height;

               _ad.VerticalAlignment = ConvertVerticalAlignment(vertAlign);
               _ad.HorizontalAlignment = ConvertHorizontalAlignment(horizontalAlign);


#if WINDOWS_PHONE

               baseGrid.Children.Add(_ad);

               if(baseGrid.Children.Count >0)
                    _adIndex = baseGrid.Children.Count - 1;
               

#elif NETFX_CORE
               backPanel.Children.Add(_ad);

               if (backPanel.Children.Count > 0)
                   _adIndex = backPanel.Children.Count - 1;
           
#endif
               _ad.ErrorOccurred += OnAdError_ErrorOccurred;

               _ad.AdRefreshed += OnAdRefreshed;

               _isBuilt = true;
               return;
           }
           catch (Exception e)
           {
               _errorMessage = "PubCenter Error: " + e.Message;
               _isBuilt = false;
           }
#else
            //function will return and do nothing if its in Unity editor
            _isBuilt = false;
#endif
        }
        //This function is public and called from Unity
        //It then uses the dispatcher to invoke on the UI thread

        /*<Summary>
         * Windows ad service overload
        </Summery>*/
        public void CreateAd(string apId, string unitId, double height, double width, bool autoRefresh, HORIZONTAL_ALIGNMENT horizontalAlign, VERTICAL_ALIGNMENT vertAlign)
        {
#if WINDOWS_PHONE || NETFX_CORE
            Dispatcher.InvokeOnUIThread(() =>
                {
                   BuildAd(apId, unitId, height, width, autoRefresh,horizontalAlign,vertAlign);
                });
#endif
        }