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

namespace meteo
{
    public partial class Webserver : Form
    {

        Bitmap image1;
        private Graphics graX;
        Bitmap snih = new Bitmap("pozadi2.png");


        int interval,dnu = 0,cas_trend;
        //string t_max, t_min, datum_t_max, datum_t_min, tl_max, tl_min, datum_tl_max, datum_tl_min, vt_max, datum_vt_max, datum_srazk_max, sraz_max_den;
        string datum_t_max, datum_t_min, datum_tl_max, datum_tl_min, datum_vt_max, datum_srazk_max, datum_vlh_max, datum_vlh_min;
        decimal t_max, t_min, tl_max, tl_min, vt_max, sraz_max_den, vlh_max_celk, vlh_min_celk;

        public Webserver()
        {
            InitializeComponent();
        }

       
        
       

       

        private void bt_smaz_Click(object sender, EventArgs e)
        {
            try
            {
                File.Delete(@"Windows/www/wwwpub/amline/data2.txt");
                File.Delete(@"Windows/www/wwwpub/amline/data.txt");
                MessageBox.Show("Zaznamy smazany" , "Hlaseni");
            }
            catch 
            { 
            }

        }

      

        private void on_load(object sender, EventArgs e)
        {
            image1 = new Bitmap(800, 480);
            graX = Graphics.FromImage(image1);
            pictureBox1.Image = image1;
            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);
              
            //string tempFile = Path.GetTempFileName();
            object[] row;
            StreamReader sr = new StreamReader(@"log.txt", true);
           
            {
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line != "")
                        {
                            row = line.Split(';');
                            
                            interval = int.Parse(row[0].ToString());
                            dnu = int.Parse(row[1].ToString());
                            t_max = decimal.Parse(row[2].ToString());
                            datum_t_max = row[3].ToString();
                            t_min = decimal.Parse(row[4].ToString());
                            datum_t_min = row[5].ToString();
                            tl_max = decimal.Parse(row[6].ToString());
                            datum_tl_max = row[7].ToString();
                            tl_min = decimal.Parse(row[8].ToString());
                            datum_tl_min = row[9].ToString();
                            vt_max = decimal.Parse(row[10].ToString());
                            datum_vt_max = row[11].ToString();
                            sraz_max_den = decimal.Parse(row[12].ToString());
                            datum_srazk_max = row[13].ToString();
                            vlh_max_celk = decimal.Parse(row[14].ToString());
                            datum_vlh_max = row[15].ToString();
                            vlh_min_celk = decimal.Parse(row[16].ToString());
                            datum_vlh_min = row[17].ToString();
                            cas_trend = int.Parse(row[18].ToString());
                        }
                    }

                }
                //graX.DrawString("Rekordy a nastavení meteostanice", new Font("Tahoma", 30, FontStyle.Regular), new SolidBrush(Color.Black), 100, 20);
                graX.DrawString("Interval generování dat (min):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 80);
                graX.DrawString("počet dnů pro generování grafů (n):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 110);
                graX.DrawString("počet hodin pro výpočet tendence (hod):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 140);


                graX.DrawString("Teplota maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 230);
                graX.DrawString(t_max + " °C dne " + datum_t_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 230);
                graX.DrawString("Teplota minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 260);
                graX.DrawString(t_min + " °C dne " + datum_t_min, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 260);
                graX.DrawString("Vlhkost maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 290);
                graX.DrawString(vlh_max_celk + " % dne " + datum_vlh_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 290);
                graX.DrawString("Vlhkost minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 320);
                graX.DrawString(vlh_min_celk + " % dne " + datum_vlh_min, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 320);
                graX.DrawString("Tlak maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 350);
                graX.DrawString(tl_max + " hPa dne " + datum_tl_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 350);
                graX.DrawString("Tlak minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 380);
                graX.DrawString(tl_min + " hPa dne " + datum_tl_min, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 380);
                graX.DrawString("Vítr maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 410);
                graX.DrawString(vt_max + " m/s dne " + datum_vt_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 410);
                graX.DrawString("Srážky maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 440);
                graX.DrawString(sraz_max_den + " mm dne " + datum_srazk_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 440);

                pictureBox1.Image = image1;

            }
            sr.Close();
            File.Delete(@"log.txt");

            Nud1.Value = interval;
            Nud2.Value = dnu;
            Nud3.Value = cas_trend;
          
           
        }

        private void Nup1_val_changed(object sender, EventArgs e)
        {
        
            interval = (int)Nud1.Value;
            uloz();
                              
        }

        private void Nup3_val_changed(object sender, EventArgs e)
        {

            cas_trend = (int)Nud3.Value;
            uloz();

        }

        void uloz() {

            StreamWriter sw = new StreamWriter(@"log.txt");
            sw.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14};{15};{16};{17};{18}", interval, dnu, t_max, datum_t_max, t_min, datum_t_min, tl_max, datum_tl_max, tl_min, datum_tl_min, vt_max, datum_vt_max, sraz_max_den, datum_srazk_max, vlh_max_celk, datum_vlh_max, vlh_min_celk, datum_vlh_min, cas_trend);
            sw.Flush();
            sw.Close();
        
        }

        private void Nup2_val_changed(object sender, EventArgs e)
        {
            dnu = (int)Nud2.Value;
            uloz();
        }

        private void bt_menu_Click(object sender, EventArgs e)
        {
            uloz();
            this.Close();
        }

      
        private void bt_rekordy_Click(object sender, EventArgs e)  //maze rekordy
        {
            
            StreamWriter sw = new StreamWriter(@"log.txt");
            //t_max = "0"; datum_t_max = "N/A"; t_min = "30"; datum_t_min = "N/A"; tl_max = "0"; datum_tl_max = "N/A"; tl_min = "1050"; datum_tl_min = "N/A"; vt_max = "0"; datum_vt_max = "N/A"; sraz_max_den = "0"; datum_srazk_max = "N/A";
            t_max = -30.1m; datum_t_max = "N/A"; t_min = 30.1m; datum_t_min = "N/A"; tl_max = 0; datum_tl_max = "N/A"; tl_min = 1050.1m; datum_tl_min = "N/A"; vt_max = -2.0m; datum_vt_max = "N/A"; sraz_max_den = -1.0m; datum_srazk_max = "N/A"; vlh_max_celk = 0; datum_vlh_max = "N/A"; vlh_min_celk = 99; datum_vlh_min = "N/A";
            sw.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14};{15};{16};{17};{18}", interval, dnu, t_max, datum_t_max, t_min, datum_t_min, tl_max, datum_tl_max, tl_min, datum_tl_min, vt_max, datum_vt_max, sraz_max_den, datum_srazk_max, vlh_max_celk, datum_vlh_max, vlh_min_celk, datum_vlh_min,cas_trend);
            sw.Flush();
            sw.Close();


            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);
            //graX.DrawString("Rekordy a nastavení meteostanice", new Font("Tahoma", 30, FontStyle.Regular), new SolidBrush(Color.Black), 100, 20);
            graX.DrawString("Interval generování dat (min):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 80);
            graX.DrawString("počet dnů pro generování grafů (n):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 110);
            graX.DrawString("počet hodin pro výpočet tendence (hod):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 140);

            graX.DrawString("Teplota maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 230);
            graX.DrawString(t_max + " °C dne " + datum_t_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 230);
            graX.DrawString("Teplota minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 260);
            graX.DrawString(t_min + " °C dne " + datum_t_min, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 260);
            graX.DrawString("Vlhkost maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 290);
            graX.DrawString(vlh_max_celk + " % dne " + datum_vlh_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 290);
            graX.DrawString("Vlhkost minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 320);
            graX.DrawString(vlh_min_celk + " % dne " + datum_vlh_min, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 320);
            graX.DrawString("Tlak maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 350);
            graX.DrawString(tl_max + " hPa dne " + datum_tl_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 350);
            graX.DrawString("Tlak minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 380);
            graX.DrawString(tl_min + " hPa dne " + datum_tl_min, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 380);
            graX.DrawString("Vítr maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 410);
            graX.DrawString(vt_max + " m/s dne " + datum_vt_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 410);
            graX.DrawString("Srážky maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 440);
            graX.DrawString(sraz_max_den + " mm dne " + datum_srazk_max, new Font("Tahoma", 15, FontStyle.Bold), new SolidBrush(Color.Black), 250, 440);
            pictureBox1.Image = image1;

            MessageBox.Show("Rekordy smazány", "Hlášení");

        }

       

        

     

        
        
    }
}