using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TerritorySite.BL.CMS;
using TerritorySite.BL;
using TerritorySite.BL.Mapquest;
using TerritorySite.BL.Geocoding;
public partial class Registered_ManageTerritoryAddresses : System.Web.UI.Page
{
    private TerritorySite.BL.CMS.Territory thisTerritory;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (CurrentUser.CurrentAccount.IsForeignLanguage.Value)
        {
            PanelContent.Visible = true;
            LabelError.Visible = false;



            if (CurrentUser.TerritoryId == 0)
            {
                Response.Redirect("Default.aspx");
            }
            thisTerritory = new TerritorySite.BL.CMS.Territory(CurrentUser.TerritoryId);
            if (!Page.IsPostBack)
            {
                PageHeader1.Text = "Edit addresses for territory #" + thisTerritory.TerritoryNumber;
                DoDataBind();
            }
            //this.TextBoxStreet.Attributes.Add("onfocus", "javascript:somethingChanged();");
            //this.DropDownListCities.Attributes.Add("onfocus", "javascript:somethingChanged();");
            ////this.TextBoxHouseNumber.Attributes.Add("onfocus", "javascript:somethingChanged();");
            //this.TextBoxApartmentNumber.Attributes.Add("onfocus", "javascript:somethingChanged();");
            //this.DropDownListLanguages.Attributes.Add("onfocus", "javascript:somethingChanged();");
        }
        else
        {
            PanelContent.Visible = false;
            LabelError.Visible = true;
        }
    }

    private void DoDataBind()
    {

        DropDownListCities.DataSource = City.GetForCurrentAccount();
        DropDownListCities.DataBind();
        DropDownListLanguages.DataSource = Language.GetForCurrentAccount();
        DropDownListLanguages.DataBind();
        LabelName.Visible = CurrentUser.CurrentAccount.TrackNames.Value;
        TextBoxFirstName.Visible = CurrentUser.CurrentAccount.TrackNames.Value;
        TextBoxLastName.Visible = CurrentUser.CurrentAccount.TrackNames.Value;
        LabelPhoneNumber.Visible = CurrentUser.CurrentAccount.TrackPhoneNumbers.Value;
        TextBoxPhoneNumber.Visible = CurrentUser.CurrentAccount.TrackPhoneNumbers.Value;
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        AddressSearchCollection foundItems = Address.Search(TextBoxStreet.Text.Trim(),
            int.Parse(DropDownListCities.SelectedValue),
            int.Parse(TextBoxHouseNumber.Text),
            TextBoxApartmentNumber.Text);
        if (foundItems.Count == 1)
        {
            Address foundAddress = new Address(foundItems[0].AddressId);
            foundAddress.LanguageId = int.Parse(DropDownListLanguages.SelectedValue);
            if (foundAddress.LongX == string.Empty | foundAddress.LongX == "0")
            {
                string lat = string.Empty;
                string lng = string.Empty;
                Geocode(ref lng, ref lat);
                foundAddress.LongX = lng;
                foundAddress.Lat = lat;

            }
            foundAddress.FirstName = TextBoxFirstName.Text;
            foundAddress.LastName = TextBoxLastName.Text;
            foundAddress.PhoneNumber = TextBoxPhoneNumber.Text;


            foundAddress.Save();
            LabelMessage.Text = "Address updated";
            TextBoxLastName.Text = string.Empty;
            TextBoxFirstName.Text = string.Empty;
            TextBoxPhoneNumber.Text = string.Empty;
            TextBoxApartmentNumber.Text = string.Empty;
        }
        else if (foundItems.Count == 0)
        {
            bool geocoded = false;
            string lat = string.Empty;
            string lng = string.Empty;
            geocoded = Geocode(ref lng, ref lat);
            Address newAddress = new Address();
            newAddress.StreetName = TextBoxStreet.Text.Trim();
            newAddress.TerritoryId = CurrentUser.TerritoryId;
            newAddress.CityId = int.Parse(DropDownListCities.SelectedValue);
            newAddress.LanguageId = int.Parse(DropDownListLanguages.SelectedValue);
            newAddress.ApartmentNumber = TextBoxApartmentNumber.Text.Trim();
            newAddress.HouseNumber = int.Parse(TextBoxHouseNumber.Text);
            newAddress.LongX = lng;
            newAddress.Lat = lat;
            newAddress.FirstName = TextBoxFirstName.Text;
            newAddress.LastName = TextBoxLastName.Text;
            newAddress.PhoneNumber = TextBoxPhoneNumber.Text;
            newAddress.Save();
            LabelMessage.Text = "Address added";
            TextBoxLastName.Text = string.Empty;
            TextBoxFirstName.Text = string.Empty;
            TextBoxPhoneNumber.Text = string.Empty;
            TextBoxApartmentNumber.Text = string.Empty;
            if (!geocoded)
            {
                LabelMessage.Text = "Address was added but we were unable to map it";
            }
        }
        else
        {
            LabelMessage.Text = "There are multiple addresses matching your criteria: you must narrow it down to zero or one";
        }



    }

    private bool Geocode(ref  string lng, ref string lat)
    {
        bool ret = false;
        // PAOLO WAS HERE
        throw new NotImplementedException("Must revisit");
        City thisCity = new City(int.Parse(DropDownListCities.SelectedValue));
        string address = this.TextBoxHouseNumber.Text + " " + TextBoxStreet.Text + ", " + DropDownListCities.SelectedItem.Text + " " + thisCity.StateProvince.Description;

        try
        {
            var geocoder = new GeocodingProxy();
            Coordinate coords = geocoder.Geocode(address);
            lat = coords.Latitude.ToString();
            lng = coords.Longitude.ToString();
            ret = true;
        }
        catch (Exception ex)
        {

        }
        return ret;


    }

}
