//------------------------------------------------------------------------------
// 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;

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

        private void TogglePanels()
        {
            pnlTaxDisplayType.Visible = !cbAllowCustomersToSelectTaxDisplayType.Checked;

            pnlShippingPriceIncludesTax.Visible = cbShippingIsTaxable.Checked;
            pnlShippingTaxClass.Visible = cbShippingIsTaxable.Checked;

            pnlPaymentMethodAdditionalFeeIncludesTax.Visible = cbPaymentMethodAdditionalFeeIsTaxable.Checked;
            pnlPaymentMethodAdditionalFeeTaxClass.Visible = cbPaymentMethodAdditionalFeeIsTaxable.Checked;
        }

        protected void FillDropDowns()
        {
            CommonHelper.FillDropDownWithEnum(this.ddlTaxDisplayType, typeof(TaxDisplayTypeEnum));
            CommonHelper.FillDropDownWithEnum(this.ddlTaxBasedOn, typeof(TaxBasedOnEnum));
            
            TaxCategoryCollection taxCategoryCollection = TaxCategoryManager.GetAllTaxCategories();

            this.ddlShippingTaxClass.Items.Clear();
            ListItem itemShippingTaxCategory = new ListItem("---", "0");
            this.ddlShippingTaxClass.Items.Add(itemShippingTaxCategory);
            foreach (TaxCategory taxCategory in taxCategoryCollection)
            {
                ListItem item2 = new ListItem(taxCategory.Name, taxCategory.TaxCategoryID.ToString());
                this.ddlShippingTaxClass.Items.Add(item2);
            }

            this.ddlPaymentMethodAdditionalFeeTaxClass.Items.Clear();
            ListItem itemPaymentMethodAdditionalFeeTaxCategory = new ListItem("---", "0");
            this.ddlPaymentMethodAdditionalFeeTaxClass.Items.Add(itemPaymentMethodAdditionalFeeTaxCategory);
            foreach (TaxCategory taxCategory in taxCategoryCollection)
            {
                ListItem item2 = new ListItem(taxCategory.Name, taxCategory.TaxCategoryID.ToString());
                this.ddlPaymentMethodAdditionalFeeTaxClass.Items.Add(item2);
            }
        }

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

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

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

        private void BindData()
        {
            cbPricesIncludeTax.Checked = TaxManager.PricesIncludeTax;
            cbAllowCustomersToSelectTaxDisplayType.Checked = TaxManager.AllowCustomersToSelectTaxDisplayType;
            CommonHelper.SelectListItem(this.ddlTaxDisplayType, (int)TaxManager.TaxDisplayType);
            cbDisplayTaxSuffix.Checked = TaxManager.DisplayTaxSuffix;
            cbHideZeroTax.Checked = TaxManager.HideZeroTax;
            cbHideTaxInOrderSummary.Checked = TaxManager.HideTaxInOrderSummary;
            CommonHelper.SelectListItem(this.ddlTaxBasedOn, (int)TaxManager.TaxBasedOn);

            Address defaultTaxAddress = TaxManager.DefaultTaxAddress;
            this.FillCountryDropDowns();
            CommonHelper.SelectListItem(this.ddlTaxDefaultCountry, defaultTaxAddress.CountryID);
            this.FillStateProvinceDropDowns();
            CommonHelper.SelectListItem(this.ddlTaxDefaultStateProvince, defaultTaxAddress.StateProvinceID);
            txtTaxDefaultZipPostalCode.Text = defaultTaxAddress.ZipPostalCode;

            cbShippingIsTaxable.Checked = TaxManager.ShippingIsTaxable;
            cbShippingPriceIncludesTax.Checked = TaxManager.ShippingPriceIncludesTax;
            CommonHelper.SelectListItem(this.ddlShippingTaxClass, TaxManager.ShippingTaxClassID);

            cbPaymentMethodAdditionalFeeIsTaxable.Checked = TaxManager.PaymentMethodAdditionalFeeIsTaxable;
            cbPaymentMethodAdditionalFeeIncludesTax.Checked = TaxManager.PaymentMethodAdditionalFeeIncludesTax;
            CommonHelper.SelectListItem(this.ddlPaymentMethodAdditionalFeeTaxClass, TaxManager.PaymentMethodAdditionalFeeTaxClassID);
        }

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

        protected void cbAllowCustomersToSelectTaxDisplayType_CheckedChanged(object sender, EventArgs e)
        {
            TogglePanels();
        }

        protected void cbShippingIsTaxable_CheckedChanged(object sender, EventArgs e)
        {
            TogglePanels();
        }

        protected void cbPaymentMethodAdditionalFeeIsTaxable_CheckedChanged(object sender, EventArgs e)
        {
            TogglePanels();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    TaxManager.PricesIncludeTax = cbPricesIncludeTax.Checked;
                    TaxManager.AllowCustomersToSelectTaxDisplayType = cbAllowCustomersToSelectTaxDisplayType.Checked;

                    TaxManager.TaxDisplayType = (TaxDisplayTypeEnum)Enum.ToObject(typeof(TaxDisplayTypeEnum), int.Parse(this.ddlTaxDisplayType.SelectedItem.Value));
                    TaxManager.DisplayTaxSuffix = cbDisplayTaxSuffix.Checked;
                    TaxManager.HideZeroTax= cbHideZeroTax.Checked;
                    TaxManager.HideTaxInOrderSummary = cbHideTaxInOrderSummary.Checked; 
                    TaxManager.TaxBasedOn = (TaxBasedOnEnum)Enum.ToObject(typeof(TaxBasedOnEnum), int.Parse(this.ddlTaxBasedOn.SelectedItem.Value));

                    Address defaultTaxAddress = new Address();
                    defaultTaxAddress.CountryID = int.Parse(this.ddlTaxDefaultCountry.SelectedItem.Value);
                    defaultTaxAddress.StateProvinceID = int.Parse(this.ddlTaxDefaultStateProvince.SelectedItem.Value);
                    defaultTaxAddress.ZipPostalCode = txtTaxDefaultZipPostalCode.Text;
                    TaxManager.DefaultTaxAddress = defaultTaxAddress;

                    TaxManager.ShippingIsTaxable = cbShippingIsTaxable.Checked;
                    TaxManager.ShippingPriceIncludesTax = cbShippingPriceIncludesTax.Checked;
                    TaxManager.ShippingTaxClassID = int.Parse(this.ddlShippingTaxClass.SelectedItem.Value);

                    TaxManager.PaymentMethodAdditionalFeeIsTaxable = cbPaymentMethodAdditionalFeeIsTaxable.Checked;
                    TaxManager.PaymentMethodAdditionalFeeIncludesTax = cbPaymentMethodAdditionalFeeIncludesTax.Checked;
                    TaxManager.PaymentMethodAdditionalFeeTaxClassID = int.Parse(this.ddlPaymentMethodAdditionalFeeTaxClass.SelectedItem.Value);

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

    }
}