//StealthNet
//Copyright (C) 2010 T.Norad

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Net;
using System.IO;

namespace Regensburger.RShare
{
    public static class GeoIP2GUIWrapper
    {
        /// <summary>
        /// Instance of the CountryLookup.
        /// 
        /// This Instance needs the geoip-binary-file. Download the latest version from:
        /// http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
        /// </summary>
        private static CountryLookup m_IPLookup = new CountryLookup(Properties.Resources.GeoIP);

        private static String geoIPServiceURL = "http://geoip.stealthnet.de/";

        private static String NA_FLAG = "--";

        public static Image lookupCountryImage(string ipaddress)
        {
            try
            {
                if (ipaddress != null && m_IPLookup != null)
                {
                    String countryCode = m_IPLookup.lookupCountryCode(ipaddress);
                    // check the country code with the geoip service
                    if (NA_FLAG.Equals(countryCode))
                    {
                        countryCode = webLookupCountryCode(ipaddress);
                    }
                    if (countryCode != null)
                    {
                        Object obj = Properties.Resources.ResourceManager.GetObject("flag_" + countryCode.ToLower());
                        if (obj != null && obj is Image)
                        {
                            return (Image)obj;
                        }
                    }
                }
                return Properties.Resources.flag___;
            } catch {
                return Properties.Resources.flag___;
            }
        }

        public static String lookupCountryCode(string ipaddress)
        {
            try
            {
                if (ipaddress != null && m_IPLookup != null)
                {
                    String countryCode = m_IPLookup.lookupCountryCode(ipaddress);
                    if (countryCode != null)
                    {
                        return countryCode;
                    }
                }
                // check the country code with the geoip service
                return webLookupCountryCode(ipaddress);
            }
            catch
            {
                return NA_FLAG;
            }
        }

        private static String webLookupCountryCode(string ipaddress)
        {
            try
            {
                // Create a request for the URL. 
                WebRequest request = WebRequest.Create(geoIPServiceURL + "?ipaddress=" + ipaddress + "&sn_version=" + Core.Version);
                // Get the response.
                ((HttpWebRequest)request).UserAgent = "stealthnet client";
                WebResponse response = request.GetResponse();
                
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                //Console.WriteLine(responseFromServer);
                // Clean up the streams and the response.
                reader.Close();
                response.Close();

                return responseFromServer.Substring(responseFromServer.IndexOf("00=") + 3, 2);
            }
            catch
            {
                return NA_FLAG;
            }
        }
    }
}
