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;


namespace iTripLog.WeatherF
{
    public partial class FrmCityList : Form
    {
        DataSet ds = new DataSet();
        public FrmCityList()
        {
            InitializeComponent();
        }



        private void FrmCityList_Load(object sender, EventArgs e)
        {
           
            ShowCitiesWeather();
           

        }

        private void ShowCitiesWeather()
        {
            Cursor.Current = Cursors.WaitCursor;
            Cursor.Show();
            if (File.Exists(Common.AppFilePath + "\\CityList.xml"))
            {
                ds.ReadXml(Common.AppFilePath + "\\CityList.xml");
            }

            if ((ds == null) || (ds.Tables.Count == 0))
            {
                FrmAddCity frm = new FrmAddCity();
                frm.Show();
                this.Hide();
            }


            for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
            {
                string cityCode = ds.Tables[0].Rows[i - 1][0].ToString();
                string[] weather = Common.GetWebService().getWeather(cityCode, "");

                Bitmap myImage = null;
                int hour = DateTime.Now.Hour;
                if ((hour >= 6) && (hour <= 18))
                    myImage = new Bitmap(Common.AppFilePath + "\\images\\b_" + weather[10].Trim());
                else
                    myImage = new Bitmap(Common.AppFilePath + "\\images\\b_" + weather[11].Trim());

                int index;

                switch (i)
                {
                    case 1:
                        pictureBox1.Image = (Image)myImage; pictureBox1.Visible = true;
                        index = weather[0].IndexOf(' ');
                        label1.Text = weather[0].Trim().Substring(index + 1); label1.Visible = true;
                        label11.Text = weather[8].Trim(); label11.Visible = true;
                        checkBox1.Visible = true; 
                        break;
                    case 2:
                        pictureBox2.Image = (Image)myImage; pictureBox2.Visible = true;
                        index = weather[0].IndexOf(' ');
                        label2.Text = weather[0].Trim().Substring(index + 1); label2.Visible = true;
                        label22.Text = weather[8].Trim(); label22.Visible = true;
                        checkBox2.Visible = true; 
                        break;
                    case 3:
                        pictureBox3.Image = (Image)myImage; pictureBox3.Visible = true;
                        index = weather[0].IndexOf(' ');
                        label3.Text = weather[0].Trim().Substring(index + 1); label3.Visible = true;
                        label33.Text = weather[8].Trim(); label33.Visible = true;
                        checkBox3.Visible = true; 
                        break;
                    case 4:
                        pictureBox4.Image = (Image)myImage; pictureBox4.Visible = true;
                        index = weather[0].IndexOf(' ');
                        label4.Text = weather[0].Trim().Substring(index + 1); label4.Visible = true;
                        label44.Text = weather[8].Trim(); label44.Visible = true;
                        checkBox4.Visible = true; 
                        break;
                    case 5:
                        pictureBox5.Image = (Image)myImage; pictureBox5.Visible = true;
                        index = weather[0].IndexOf(' ');
                        label5.Text = weather[0].Trim().Substring(index + 1); label5.Visible = true;
                        label55.Text = weather[8].Trim(); label55.Visible = true;
                        checkBox5.Visible = true; 
                        break;


                }

                myImage = null;


            }

            Cursor.Current = Cursors.Default; 
        }

        private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.ImageIndex)
            {
                case 0: //显示天气明细

                    if (string.IsNullOrEmpty(GetSelectedCityCode()))
                        Common.CityCode = ds.Tables[0].Rows[0][0].ToString();
                    else
                        Common.CityCode = GetSelectedCityCode();

                    FrmDefaultCityWeather frm = new FrmDefaultCityWeather();
                    frm.Show();
                    this.Hide();
                    break;
                case 1: //添加城市
                    FrmAddCity frmAdd = new FrmAddCity();
                    frmAdd.Show();
                    this.Hide();
                    break;
                case 2: //减少城市
                    break;
                case 3: //天气更新
                    ShowCitiesWeather();
                    break;
            }

        }

        private void checkBox1_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false; 
            
            }
        }

        private void checkBox2_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox1.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;

            }
        }

        private void checkBox3_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                checkBox2.Checked = false;
                checkBox1.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;

            }
        }

        private void checkBox4_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox1.Checked = false;
                checkBox5.Checked = false;

            }
        }

        private void checkBox5_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox1.Checked = false;

            }
        }

        private string GetSelectedCityCode()
        { 
            string ret=string.Empty;
            if (checkBox1.Checked) ret = ds.Tables[0].Rows[0][0].ToString();
            if (checkBox2.Checked) ret = ds.Tables[0].Rows[1][0].ToString();
            if (checkBox3.Checked) ret = ds.Tables[0].Rows[2][0].ToString();
            if (checkBox4.Checked) ret = ds.Tables[0].Rows[3][0].ToString();
            if (checkBox5.Checked) ret = ds.Tables[0].Rows[4][0].ToString();

            return ret;

        }
    }
}