using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace iTripLog.WeatherF
{
    public partial class FrmAddCity : Form
    {

        private DataSet dsRegion = new DataSet();
        private DataSet dsCity = new DataSet();

        public FrmAddCity()
        {
            InitializeComponent();
        }


        private void cbProvince_SelectedIndexChanged(object sender, EventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            Cursor.Show();
            dsCity = Common.GetWebService().getSupportCityDataset(cbProvince.SelectedValue.ToString());

            //

            cbCity.DisplayMember = "CityName";
            cbCity.ValueMember = "CityId";
            cbCity.DataSource = dsCity.Tables[0];

            Cursor.Current = Cursors.Default; 

        }

        private void FrmAddCity_Load(object sender, EventArgs e)
        {

            if (!File.Exists(Common.AppFilePath + "\\Province.xml"))
            {
                dsRegion = Common.GetWebService().getRegionDataset();
                dsRegion.WriteXml(Common.AppFilePath + "\\Province.xml");
            }
            else
            {
                dsRegion.ReadXml(Common.AppFilePath + "\\Province.xml");

            }

            cbProvince.DisplayMember = "RegionName";
            cbProvince.ValueMember = "RegionID";
            cbProvince.DataSource = dsRegion.Tables["Province"];

        }

        private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.ImageIndex)
            {
                case 0://return
                    FrmCityList frm = new FrmCityList();
                    frm.Show();
                    this.Hide();
                    break;

                case 1: //Confirm to Add
                    AddCityItemToXmlFile();

                    break;

                case 2: //Stop to Run
                    Application.Exit();
                    break;

            }



        }

        private void AddCityItemToXmlFile()
        {
            DataSet ds = new DataSet();
            if (File.Exists(Common.AppFilePath + "\\CityList.xml"))
            {
                ds.ReadXml(Common.AppFilePath + "\\CityList.xml");

                DataRow dr = ds.Tables[0].NewRow();
                dr["CityID"] = cbCity.SelectedValue.ToString();
                dr["CityName"] = cbCity.Text;
                ds.Tables[0].Rows.Add(dr);

                //

                ds.WriteXml(Common.AppFilePath + "\\CityList.xml");

            }


        }
    }
}