using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;//www.gorselprogramlama.com
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;

namespace Namaz_Vakit_Hesaplama
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OleDbConnection connect = new OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=vakitler.accdb;Jet OLEDB:Database Password=qcs3swkw89;");

        string tarih = "";

        void baglantiKur()
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();//www.gorselprogramlama.com
            }
        }

        void listviewItemsAdds(ListView lv, params string[] item)
        {
            for (int i = 0; i < item.Length; i++)
            {
                lv.Items.Add(item[i]);
            }
        }

        private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            baglantiKur();//www.gorselprogramlama.com
            string sql = "SELECT * FROM ayarlar WHERE ayar_Adi = 'yaz_saati'";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            OleDbDataReader oku = cmd.ExecuteReader();
            while (oku.Read())
            {
                string sonuc = oku[2].ToString();

                if (sonuc == "evet")
                {
                    rbEvet_y.Checked = true;
                }
                else
                {
                    rbHayir_y.Checked = true;
                }
            }
            connect.Close();

            lblTarih.Text = DateTime.Now.ToLongDateString();
            lblSaat.Text = DateTime.Now.ToLongTimeString();            
            tmrVakit.Enabled = true;
            listviewItemsAdds(listView1, "İmsak", "Güneş", "Öğle", "İkindi", "Akşam", "Yatsı");
            listviewItemsAdds(listView2, "İmsak", "Güneş", "Öğle", "İkindi", "Akşam", "Yatsı");
            tarih = DateTime.Now.ToLongDateString();
            vakitGuncelle(listView1, DateTime.Now.ToString("MMMM"),DateTime.Now.ToString("dd"));
            vakitGuncelle(listView2, DateTime.Now.ToString("MMMM"), DateTime.Now.ToString("dd"));            
        }
        //www.gorselprogramlama.com
        void vakitGuncelle(ListView lv,string ay , string kaci)
        {
            //DateTime.Now.ToString("MMMM")
            //DateTime.Now.ToString("dd")
            string sql = "SELECT * FROM istanbul WHERE ayin_Adi='" + ay + "' AND ayin_Kaci=" + kaci;
            baglantiKur();
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            OleDbDataReader oku = cmd.ExecuteReader();
            while (oku.Read())
            {
                for (int i = 0; i < 6; i++)
                {                    
                    try
                    {
                        lv.Items[i].SubItems.RemoveAt(1);                        
                    }
                    catch { }
                    if (rbEvet_y.Checked)
                    {
                        TimeSpan ekle = new TimeSpan();
                        ekle = TimeSpan.Parse(oku[i + 2].ToString()) + TimeSpan.Parse("01:00");
                        string[] saat = ekle.ToString().Split(':');
                        lv.Items[i].SubItems.Add(saat[0] + ":" + saat[1]);
                    }
                    else
                    {
                        lv.Items[i].SubItems.Add(oku[i + 2].ToString());
                    }//www.gorselprogramlama.com
                }
                break;
            }
            connect.Close();
        }

        private void tmrVakit_Tick(object sender, EventArgs e)
        {          
            lblTarih.Text = DateTime.Now.ToLongDateString();
            lblSaat.Text = DateTime.Now.ToLongTimeString();
            if (tarih != lblTarih.Text)
            {
                vakitGuncelle(listView1, DateTime.Now.ToString("MMMM"), DateTime.Now.ToString("dd"));
                tarih = DateTime.Now.ToLongDateString();
            }
            enYakinZaman();
        }

        void zamanFarki(string ilkVakit , string sonrakiVakit,string adi)
        {
            DateTime ilkdeger = Convert.ToDateTime(ilkVakit);           
            DateTime sondeger = Convert.ToDateTime(sonrakiVakit);
            TimeSpan fark = sondeger.Subtract(ilkdeger);
            string[] tam = fark.ToString().Split('.');//www.gorselprogramlama.com
            lblBilgi.Text = adi + " vaktine kalan süre :   " + tam[0];
            if (tam[0] == "00:00:01")
            {
                notifyIcon1.BalloonTipText = adi +  " vakti gelmiştir. Saat : " + DateTime.Now.ToLongTimeString();
                notifyIcon1.ShowBalloonTip(30000);
            }

        }

        void enYakinZaman()
        {
            bool hata = true;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (String.Compare(listView1.Items[i].SubItems[1].Text, DateTime.Now.ToLongTimeString()) == 1)
                {
                    zamanFarki(DateTime.Now.ToLongTimeString(), listView1.Items[i].SubItems[1].Text, listView1.Items[i].SubItems[0].Text);
                    hata = false;
                    break;
                }
                //listView1.Items[i].SubItems[1].Text                
            }
            if (hata)
            {
                if (String.Compare(listView1.Items[0].SubItems[1].Text, DateTime.Now.ToLongTimeString()) == 1) // 00:00
                {
                    zamanFarki(DateTime.Now.ToLongTimeString(), listView1.Items[0].SubItems[1].Text, listView1.Items[0].SubItems[0].Text);
                }
                else
                {
                    //DateTime.Now.ToLongTimeString()
                    TimeSpan ts = Convert.ToDateTime("23:59") - Convert.ToDateTime(DateTime.Now.ToLongTimeString());
                    TimeSpan saniye_farki = Convert.ToDateTime("00:01") - Convert.ToDateTime("00:00");
                    TimeSpan sonuc = new TimeSpan();//www.gorselprogramlama.com
                    sonuc = ts + saniye_farki;
                    TimeSpan total = new TimeSpan();
                    total = TimeSpan.Parse(listView1.Items[0].SubItems[1].Text) + TimeSpan.Parse(sonuc.ToString());
                    lblBilgi.Text = "İmsak vaktine kalan süre :   " + total;
                    if (total.ToString() == "00:00:01")
                    {
                        notifyIcon1.BalloonTipText = "İmsak vakti gelmiştir. Saat : " + DateTime.Now.ToLongTimeString();
                        notifyIcon1.ShowBalloonTip(30000);
                    }
                }
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = Convert.ToDateTime(dateTimePicker1.Value);
            string ay = dt.ToString("MMMM");
            string kaci = dt.ToString("dd");
            vakitGuncelle(listView2, ay, kaci);
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            baglantiKur();
            string sql = "";
            if (rbEvet_y.Checked)
            {
                sql = "UPDATE ayarlar SET deger = 'evet' WHERE ayar_Adi = 'yaz_saati'"; 
            }
            else
            {
                sql = "UPDATE ayarlar SET deger = 'hayir' WHERE ayar_Adi = 'yaz_saati'"; 
            }

            OleDbCommand cmd = new OleDbCommand(sql, connect);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Ayarlar Kayıt Edilmiştir.","Dikkat",MessageBoxButtons.OK,MessageBoxIcon.Information);
            connect.Close();
            vakitGuncelle(listView1, DateTime.Now.ToString("MMMM"), DateTime.Now.ToString("dd"));
            DateTime dt = Convert.ToDateTime(dateTimePicker1.Value);
            string ay = dt.ToString("MMMM");
            string kaci = dt.ToString("dd");//www.gorselprogramlama.com
            vakitGuncelle(listView2, ay, kaci);

        } 
      
    }
}
