//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using NopSolutions.NopCommerce.Common.Configuration.Settings;
using NopSolutions.NopCommerce.Common.CustomerManagement;
using NopSolutions.NopCommerce.Common.Directory;
using NopSolutions.NopCommerce.Common.Tax;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Common.Shipping;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ShippingSettingsControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }


        protected void FillCountryDropDowns()
        {
            this.ddlShippingOriginCountry.Items.Clear();
            ListItem selectCountryItem = new ListItem("Select country", "0");
            this.ddlShippingOriginCountry.Items.Add(selectCountryItem);
            CountryCollection countryCollection = CountryManager.GetAllCountries();
            foreach (Country country in countryCollection)
            {
                ListItem ddlCountryItem2 = new ListItem(country.Name, country.CountryID.ToString());
                this.ddlShippingOriginCountry.Items.Add(ddlCountryItem2);
            }
        }

        protected void FillStateProvinceDropDowns()
        {
            this.ddlShippingOriginStateProvince.Items.Clear();
            int countryID = int.Parse(this.ddlShippingOriginCountry.SelectedItem.Value);

            StateProvinceCollection stateProvinceCollection = StateProvinceManager.GetStateProvincesByCountryID(countryID);
            foreach (StateProvince stateProvince in stateProvinceCollection)
            {
                ListItem ddlStateProviceItem2 = new ListItem(stateProvince.Name, stateProvince.StateProvinceID.ToString());
                this.ddlShippingOriginStateProvince.Items.Add(ddlStateProviceItem2);
            }
            if (stateProvinceCollection.Count == 0)
            {
                ListItem ddlStateProvinceItem = new ListItem("Other (Non us)", "0");
                this.ddlShippingOriginStateProvince.Items.Add(ddlStateProvinceItem);
            }
        }

        private void BindData()
        {
            cbFreeShippingOverX.Checked = SettingManager.GetSettingValueBoolean("Shipping.FreeShippingOverX.Enabled");
            txtFreeShippingOverX.Value = SettingManager.GetSettingValueDecimalNative("Shipping.FreeShippingOverX.Value");

            Address shippingOriginAddress = ShippingManager.ShippingOrigin;
            this.FillCountryDropDowns();
            CommonHelper.SelectListItem(this.ddlShippingOriginCountry, shippingOriginAddress.CountryID);
            this.FillStateProvinceDropDowns();
            CommonHelper.SelectListItem(this.ddlShippingOriginStateProvince, shippingOriginAddress.StateProvinceID);
            txtShippingOriginZipPostalCode.Text = shippingOriginAddress.ZipPostalCode;
        }

        protected void ddlShippingOriginCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillStateProvinceDropDowns();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SettingManager.SetParam("Shipping.FreeShippingOverX.Enabled", cbFreeShippingOverX.Checked.ToString());
                    SettingManager.SetParam("Shipping.FreeShippingOverX.Value", txtFreeShippingOverX.Value.ToString());

                    Address shippingOriginAddress = new Address();
                    shippingOriginAddress.CountryID = int.Parse(this.ddlShippingOriginCountry.SelectedItem.Value);
                    shippingOriginAddress.StateProvinceID = int.Parse(this.ddlShippingOriginStateProvince.SelectedItem.Value);
                    shippingOriginAddress.ZipPostalCode = txtShippingOriginZipPostalCode.Text;
                    ShippingManager.ShippingOrigin = shippingOriginAddress;

                    Response.Redirect("ShippingSettings.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

    }
}