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

using System.IO;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int gun = 1;
        int yilin_Gunu = 1;
        int ay = 0;

        OleDbConnection connect = new OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=vakitler.accdb");
                
        private void button1_Click(object sender, EventArgs e)
        {
            gun = 1;
            yilin_Gunu = 1;
            ay = 0;
            StreamReader oku = new StreamReader("1.txt", Encoding.GetEncoding(1254));
            StreamWriter yaz = new StreamWriter("2.txt");
            string okunan = oku.ReadLine();
            while (okunan != null)
            {
                if (okunan.StartsWith("//"))
                {
                    yaz.WriteLine("//" + okunan.Remove(0,2));
                    gun = 1;
                    ay++;
                }
                else 
                {
                    string[] ayir;
                    string formatli = "";
                    if (okunan.StartsWith("İmsak"))
                    {
                        yaz.WriteLine("--Ayın : " + gun + ". günü / Yılın : " + yilin_Gunu + ". günü --");
                        yaz.WriteLine("#Tarih : " + gun + "." + ay + "#");

                        ayir = okunan.Split(' ');
                        formatli = ayir[0] + "-:" + ayir[2] + ":" + ayir[3];
                        yaz.WriteLine((formatli));
                        gun++;
                        yilin_Gunu++;
                    }
                    else if (okunan.StartsWith("Güneş"))
                    {
                        ayir = okunan.Split(' ');
                        formatli = ayir[0] + "-:" + ayir[2] + ":" + ayir[3];
                        yaz.WriteLine((formatli));
                    }
                    else if (okunan.StartsWith("Öğle"))
                    {
                        ayir = okunan.Split(' ');
                        formatli = ayir[0] + "-:" + ayir[2] + ":" + ayir[3];
                        yaz.WriteLine((formatli));
                    }
                    else if (okunan.StartsWith("İkindi"))
                    {
                        ayir = okunan.Split(' ');
                        formatli = ayir[0] + "-:" + ayir[2] + ":" + ayir[3];
                        yaz.WriteLine((formatli));
                    }
                    else if (okunan.StartsWith("Akşam"))
                    {
                        ayir = okunan.Split(' ');
                        formatli = ayir[0] + "-:" + ayir[2] + ":" + ayir[3];
                        yaz.WriteLine((formatli));
                    }
                    else if (okunan.StartsWith("Yatsı"))
                    {
                        ayir = okunan.Split(' ');
                        formatli = ayir[0] + "-:" + ayir[2] + ":" + ayir[3];
                        yaz.WriteLine((formatli));                      
                    }
                }
                okunan = oku.ReadLine();
            }
            MessageBox.Show("İşlem Tamam");
            yaz.Close();
            oku.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string imsak = "",gunes = "" , ogle = "" , ikindi = "" , aksam = "" , yatsi = "";
            string ay_Adi = "";
            int ayin_Kaci = 0;

            StreamReader SR = new StreamReader("2.txt", Encoding.GetEncoding(1254));
            string okunan = SR.ReadLine();
            connect.Open();            
            while (okunan != null)
            {
                if (okunan.StartsWith("//")) // Yeni Ay
                {
                    ay_Adi = okunan.Remove(0, 2);
                }
                else if (okunan.StartsWith("#") && okunan.EndsWith("#")) // Yeni Gün
                {
                    okunan = okunan.Remove(0,9);
                    okunan = okunan.Remove(okunan.Length - 1, 1);
                    string[] bilgi = okunan.Split('.');
                    ayin_Kaci = int.Parse(bilgi[0]);
                }
                else
                {                    
                    string[] saat;
                    if (okunan.StartsWith("İmsak"))
                    {
                        saat = okunan.Split('-');
                        imsak = saat[1].Remove(0,1);
                    }
                    else if (okunan.StartsWith("Güneş"))
                    {
                        saat = okunan.Split('-');
                        gunes = saat[1].Remove(0, 1);
                    }
                    else if (okunan.StartsWith("Öğle"))
                    {
                        saat = okunan.Split('-');
                        ogle = saat[1].Remove(0, 1);
                    }
                    else if (okunan.StartsWith("İkindi"))
                    {
                        saat = okunan.Split('-');
                        ikindi = saat[1].Remove(0, 1);
                    }
                    else if (okunan.StartsWith("Akşam"))
                    {
                        saat = okunan.Split('-');
                        aksam = saat[1].Remove(0, 1);
                    }
                    else if (okunan.StartsWith("Yatsı"))
                    {
                        saat = okunan.Split('-');
                        yatsi = saat[1].Remove(0, 1);
                        string sql = "INSERT INTO istanbul VALUES('" + ay_Adi + "'," + ayin_Kaci + ",'" + imsak + "','" + gunes + "','" + ogle + "','" + ikindi + "','" + aksam + "','" + yatsi + "')";
                        OleDbCommand cmd = new OleDbCommand(sql, connect);
                        cmd.ExecuteNonQuery();
                    }
                }
                okunan = SR.ReadLine();
            }          
            SR.Close();
            connect.Close();
            MessageBox.Show("İşlem Tamam");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            connect.Open();
            int kac = 1;
            string ay_Adi = "Ekim";
            string sql = "SELECT * FROM istanbul WHERE ayin_Kaci<=27 AND ayin_Adi='" + ay_Adi + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            OleDbDataReader oku = cmd.ExecuteReader();
            while (oku.Read())
            {
                string[] saatler = new string[6];
                for (int i = 2; i <=7 ; i++)
                {
                    TimeSpan ts = Convert.ToDateTime(oku[i].ToString()) - Convert.ToDateTime("01:00");
                    string[] saat = ts.ToString().Split(':');
                    saatler[i - 2] = saat[0] + ":" + saat[1];
                }
                sql = "UPDATE istanbul SET imsak='" + saatler[0] + "' WHERE ayin_Kaci=" + kac + " AND ayin_Adi='" + ay_Adi + "'";
                cmd = new OleDbCommand(sql, connect);
                cmd.ExecuteNonQuery();

                sql = "UPDATE istanbul SET günes='" + saatler[1] + "' WHERE ayin_Kaci=" + kac + " AND ayin_Adi='" + ay_Adi + "'";
                cmd = new OleDbCommand(sql, connect);
                cmd.ExecuteNonQuery();

                sql = "UPDATE istanbul SET ogle='" + saatler[2] + "' WHERE ayin_Kaci=" + kac + " AND ayin_Adi='" + ay_Adi + "'";
                cmd = new OleDbCommand(sql, connect);
                cmd.ExecuteNonQuery();

                sql = "UPDATE istanbul SET ikindi='" + saatler[3] + "' WHERE ayin_Kaci=" + kac + " AND ayin_Adi='" + ay_Adi + "'";
                cmd = new OleDbCommand(sql, connect);
                cmd.ExecuteNonQuery();

                sql = "UPDATE istanbul SET aksam='" + saatler[4] + "' WHERE ayin_Kaci=" + kac + " AND ayin_Adi='" + ay_Adi + "'";
                cmd = new OleDbCommand(sql, connect);
                cmd.ExecuteNonQuery();

                sql = "UPDATE istanbul SET yatsi='" + saatler[5] + "' WHERE ayin_Kaci=" + kac + " AND ayin_Adi='" + ay_Adi + "'";
                cmd = new OleDbCommand(sql, connect);
                cmd.ExecuteNonQuery();

                kac++;
                
            }
            MessageBox.Show("İşlem tamam");
            connect.Close();
        }
        
    }
}
