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.Globalization;
using System.IO;
using System.Threading;

namespace Video_Club
{
    public partial class frmMain : Form
    {
        Movie[] movie; //ye araye az hameye film ha ke tedadesh to method e 'processor' moshakhas mishe
        PersianCalendar pc = new PersianCalendar(); //ye class e male khode C# vase kar ba tarikhe farsi !
        int textBoxTextLength, menuItem,numberOfMovies,movieNumber; //payin tar tozih midam !
        string textInput; //inam payin tar tozih midam
        string skin;

        public frmMain()
        {
            InitializeComponent();
        }

        void changeText(string text) //in method ro vase in dar nazar gereftim ke matne khode barname ro az voroodie karbar joda konim !
        {
            if (text != "") //age matne voroodi khali nabood
                textBox1.Text = textBox1.Text + text; //be matne dakhele 'textBox1' , matne 'text' (voroodi) ro ezafe kon
            
            textBoxTextLength = textBox1.TextLength; //dakhele moteqayere 'textBoxTextLength' ke az noe int hast toole matne 'textBox1' ro beriz
            textBox1.Select(textBoxTextLength, 0); //in method male khode 'textBox' hast va inja karesh ine ke cursor (|) ro bebare be akhare matne 'textBox'.
            textBox1.ScrollToCaret(); //inam karesh ine ke scrolle 'textBox1' ro ta akhar bebare payin
        }

        void processor(int mnuItm,string textInput) //in method pardazesh gare voroodie karbar hastesh ! 'mnuItm' moshakhas mikone che kari bayad anjam beshe va 'textInput' ham meqdare voroodie karbare
        {
            menuItem = mnuItm; //mnuItm e method e processor ro beriz to menuItem e asli (chert neveshtam ye joorayi) :|
            switch(menuItem) 
            {
                case 1: //moshakhas kardane tedade film ha
                    {
                        numberOfMovies = int.Parse(textInput); //voroodie karbar to in qesmat tedade film hast ke to numberOfMovies rikhte mishe
                        movie = new Movie[numberOfMovies]; //inja moshakhas mishe tedade khoone haye arayeye film ha chand tas !
                        
                        for (int a = 0; a < numberOfMovies;a++ ) //bad az moshakhas kardane tedade khoone haye araye meqdareshoon "NULL" hastesh ! bayad yeki yeki baraye har khoone meqdar tayin konim
                        {
                            movie[a] = new Movie(); //khooneye shomare 'a' ro ye nemoone class e 'Movie()' bendaz toosh !
                        }

                        changeText("Enter Movie Number (from 1 to " + numberOfMovies + "): "); //inja be method e 'changeText' dastoor mide in matno to 'textBox' namayesh bede 
                        menuItem = 2; // alan kare 'menuItem 1' tamoom shode va faqat yek bar avale barname estefade mishe ! hala mirim menuItem2
        
                        break;
                    }
                case 2: //mikhaym varede ye area e ye film beshim vase kar bahash !
                    {
                        if (int.Parse(textInput) <= numberOfMovies && int.Parse(textInput) > 0) //age voroodie karbar(inja voroodie karbar mishe shomare film) koochiktar mosavie tedade film haye vared shode bood va voroodie karbar bozorg tar az 0 bood
                        {
                            movieNumber = int.Parse(textInput) - 1; //shomare film to araye ha mishe voroodie karbar - 1 (chon araye az 0 ta n-1 shomare gozari mishe)
                            changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No."  + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : "); //in matno ezafe kon be 'textBox' \r\n yani boro khate bad dorost mesle Enter zadane !
                            menuItem = 20;

                            //ye seri az Item ha meqdare voroodi migiran ! oonayi ke meqdare voroodi migiran ba 201 va 202 be karbar elam mikone , ba 21 va 22 amaliate asli ro anjam mide (bad az gereftane voroodie karbar)

                        }
                        else //age sharte bala bar qarar nabood
                        {
                            changeText("Error !"); //be matne 'textBox' ino ezafe kon
                            changeText("\r\nEnter Movie Number (from 1 to " + numberOfMovies + "): "); //inam ezafe kon
                            menuItem = 2; //'menuItem ro 2 kon yani boro dobare az aval bia az karbar meqdar begir
                        }

                        break;
                    }
                    case 20: //Change input Items to Movie Items
                        {
                            if (textInput != "" && int.Parse(textInput) > 0 && int.Parse(textInput) <= 7) // age voroodie karbar beyne 1 ta 7 bood
                            {
                                menuItem = int.Parse("20" + textInput); //shomare 'menuItem' ha be soorate 20x hast (2 yani to area e ye film , 0 yani item hayi ke meqdar migiran , x ham menuitem voroodi :|)
                                
                                switch (menuItem)
                                {
                                    case 201: //karesh ine ke be karbar elam kone name filmo vared kon ! (set shodane name film to case 21 anjam mishe)
                                        {
                                            changeText("Enter Name Of Movie: ");
                                            menuItem = 21; //'menuItem' ro barabar ba itemi kon ke amaliate set kardane name film ro anjam mide
                                            break;
                                        }
                                    case 202: //karesh ine ke be karbar elam kone tedade noskhe haye mojood vase in filmo vared kon (set kardane tedad to case 22 anjam mishe)
                                        {
                                            changeText("Enter Number of This Movie: ");
                                            menuItem = 22; //'menuItem' ro barabar ba itemi kon ke amaliate set kardane tedade noskhe haye film ro anjam mide
                                            break;
                                        }
                                    case 203: //Push mikone dige ! meqdare voroodi nadare !
                                        {
                                            frmPush pushFrm = new frmPush(); //form e push ye forme ke name taraf va tarikh ro migire
                                            pushFrm.ShowDialog(); //form e push ro namayesh bede


                                            //statics ye class e static e vase enteqale etela at beyne 2 ta form estefade kardam !
                                            //class haye static ro lazem nist azashoon nemoone besazim vaqti migim masalan statics.changed = false ino har ja farakhani koni false e !


                                            if (statics.changed == true) //age meqdari vared shode bood va OK zade bood karbar
                                                changeText(movie[movieNumber].push(statics.person, statics.date)); //vase filme shomare 'movieNumber , 'Push' kon va be matne 'textBox1' javabe 'Push' ro ezafe kon! (method e 'push' meqdar bazgashti dare va az noe string e!)
                                            else //age 'changed == false' bood yani karbar cancel karde bood 
                                                changeText("\r\n***Push Process Canceled By User!***\r\n"); //ino be 'textBox1' ezafe kon


                                            statics.changed = false; //dar akharam bia taqirato false kon vase estefade haye badi!
                                            
                                            changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No."  + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : ");
                                            menuItem = 20; //dar akhar ham bia 'menuItem' ro barabar item e area e kar ba film kon
                                            break;
                                        }
                                    case 204: //Pop mikone inam :)
                                        {
                                            changeText(movie[movieNumber].pop()); //vase filme shomare 'movieNumber , 'pop' kon va javabe 'pop' ro be matne 'textBox1' ezafe kon ! (method e 'pop' meqdar bazgashti dare va az noe string e!)
                                            changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No."  + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : ");
                                            menuItem = 20; //dar akhar ham bia 'menuItem' ro barabar item e area e kar ba film kon
                                            break;
                                        }
                                    case 205: //Top
                                        {
                                            changeText(movie[movieNumber].top()); //vase filme shomare 'movieNumber , 'top' kon va natijasho be matne 'textBox1' ezafe kon ! (method e 'top' meqdar bazgashti dare va az noe string e!)
                                            changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No."  + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : ");
                                            menuItem = 20; //dar akhar ham bia 'menuItem' ro barabar item e area e kar ba film kon
                                            break;
                                        }
                                    case 206: //Print Report
                                        {
                                            changeText(movie[movieNumber].print()); //vase filme shomare 'movieNumber , report begir va natije report ro be matne 'textBox1' ezafe kon(method e 'print' meqdare bazgashti dare va az noe string e!)
                                            changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No." + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : ");
                                            menuItem = 20; //dar akhar ham bia 'menuItem' ro barabar item e area e kar ba film kon
                                            break;
                                        }
                                    case 207: //Exit From Entered Movie Area // az area e in film boro biroon
                                        {
                                            changeText("Enter Movie Number (from 1 to " + numberOfMovies + "): "); // be karbar elam kon ye filme dige entekhab kone
                                            menuItem = 2; //boro jayi ke bayad raft to area e ye film
                                            break;
                                        }
                                }
                            }
                            else
                            {
                                changeText("\r\nError!\r\nEnter your choice : ");
                                menuItem = 20;
                            }
                            break;
                        }
                            case 21: //amaliate set kardane name film ke az tariqe 'menuItem' e 201 be karbar elam kardim !
                                {
                                    movie[movieNumber].setName(textInput); //vase filme shomare 'movieNumber , name vared shode ('textInput') ro set kon ! in dige meqdar bazgashti nadare

                                    changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No." + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : ");
                                    menuItem = 20; //dar akhar ham bia 'menuItem' ro barabar item e area e kar ba film kon
                                    break;
                                }
                            case 22: //amaliate set kardane tedad noskhe haye film ke az tariqe 'menuItem' e 202 be karbar elam kardim !
                                {
                                    movie[movieNumber].setSize(int.Parse(textInput)); //vase filme shomare 'movieNumber , tedade vared shode ('textInput') ro set kon ! in dige meqdar bazgashti nadare
                                    changeText("\r\nYou Are Inside Movie No." + (movieNumber + 1).ToString() + ".\r\n1.Set Or Change Movie Name.\r\n2.Set or Change Number of This Movie.\r\n3.Push.\r\n4.Pop.\r\n5.Top.\r\n6.Print Report.\r\n7.Exit from Movie No." + (movieNumber + 1).ToString() + " Area. \r\n\r\nEnter your choice : ");
                                    menuItem = 20; //dar akhar ham bia 'menuItem' ro barabar item e area e kar ba film kon
                                    break;
                                }
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e) //vaqti ye dokme az keyboard ro feshar midim (vaqti ke focus ro 'textBox1' e)
        {
            if (e.KeyValue == 13) //age Enter zadim (keyValue e 13 = Enter)
            {
                textInput = textBox1.Text.Substring(textBoxTextLength, textBox1.TextLength - textBoxTextLength);
                /*
                 * 'textInput' meqdarie ke karbar vared karde ! va intor mohasebe mishe:
                 * kole matne 'textBox' menhaye matni ke khode barname be 'textBox' ezafe karde !
                 * har vaqt khastim khode barname ye matn ro be 'textBox' ezafe kone az method e 'textChange()' estefade kardim !
                 * va kare asli ke azash mikhaym ine ke moshakhas kone ke toole matni ke khode barname be 'textBox' ezafe mikone cheqadre !
                 * 
                 * method e SubString(shorooe kandan,toole kandan) karesh ine ke ye tike az matno bekane !
                 * bana bar in meqdare karbar = textBox1.Text.SubString(shoroo az toole matni ke barname ezafe karde (shayad fek konid +1 ham bekhad vali chon shomareshesh az 0 shoroo mishe nemikhad) , be toole (toole kole matne 'textBox1' - toole matni ke barname ezafe karde))
                */

                timer1.Enabled = true;
                /*
                 * be khatere ye seri bug haye visual studio majboor boodam ino bezaram :|
                 * bad az inke karbar meqdaro vared va enter zad va meqdar tavasote code haye bala maloom shod
                 * bayad method e 'process()' shoroo be kar kone ! ke age ma dastoore 'processor(menuItem, textInput);' ro 
                 * inja benevisim ye ekhtelali be vojood miad ! ye joorayi qable inke meqdare voroodie karbar moshakhas beshe method e 'process()'
                 * ejra mishe ! vase hamin ba ye timer ye vaqfe mindazim to ejraye method e 'process'
                 */

                if (textInput.ToLower() == "exit") //age karbar vared kard "exit" zerti az barname bepar biroon
                    Application.Exit();

            }

            if (exit == true) 
                timerExit.Enabled = true;
            //in vase khoshgelasion e ! oonja ke mige "Press Any Key to Exit ..." , moteqayere 'exit = true' mishe
            //badaesh vaqti ye dokme az keyboard ro vaqti ke focus ro 'textBox1' hast feshar bedim zerti 'timerExit'
            //ro fa al mikone!
        }

        private void timer1_Tick(object sender, EventArgs e) //hamoon vaqfe he ke goftam yekam bala tar !
        {
            processor(menuItem, textInput);
            timer1.Enabled = false;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            changeText("Please Enter Number of Movies: ");
            menuItem = 1; //jayi ke moshakhas mishe kolan chand ta film darim !

            skin = File.ReadAllText(Application.StartupPath + "\\skin.ini"); //shomare theme ro az file e skin.ini migire
            skinEngine1.SkinFile = @"skins\skin (" + skin + ").ssk";
            skinEngine1.Active = true;
            comboBox1.Text = skin;

        }


        //#########################################################################################
        //##                   Ta Injash Asle Qazie Bood Baqiash FarE !                           #
        //#########################################################################################

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
                textBox1.Width = this.Width - 20;
                textBox1.Height = this.Height - 64;
                comboBox1.Top = this.Height - 60;
                label1.Top = this.Height - 59;
                label2.Top = this.Height - 59;
                label3.Top = this.Height - 59;
                button1.Top = this.Height - 61;
                button2.Top = this.Height - 61;
                button3.Top = this.Height - 61;
                button4.Top = this.Height - 61;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString();
            label2.Text = DateTime.Now.Date.ToShortDateString() + " - " + pc.GetYear(DateTime.Now).ToString() + "/" + pc.GetMonth(DateTime.Now).ToString() + "/" + pc.GetDayOfMonth(DateTime.Now).ToString();
        }

        

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (exit == false)
            {
                e.Cancel = true;
                timer3.Enabled = true;
            }
            else
                e.Cancel = false;
        }


        int x = 0;
        bool exit = false;
        private void timer3_Tick(object sender, EventArgs e)
        {
            switch (x++)
            {
                case 1:
                    {
                        changeText("\r\n\r\nD");
                        break;
                    }
                case 2:
                    {
                        changeText("e");
                        break;
                    }
                case 3:
                    {
                        changeText("s");
                        break;
                    }
                case 4:
                    {
                        changeText("i");
                        break;
                    }

                case 5:
                    {
                        changeText("g");
                        break;
                    }
                case 6:
                    {
                        changeText("n");
                        break;
                    }
                case 7:
                    {
                        changeText("e");
                        break;
                    }
                case 8:
                    {
                        changeText("r");
                        break;
                    }
                case 9:
                    {
                        changeText("s");
                        break;
                    }
                case 10:
                    {
                        changeText(" & ");
                        break;
                    }
                case 11:
                    {
                        changeText("P");
                        break;
                    }
                case 12:
                    {
                        changeText("r");
                        break;
                    }
                case 13:
                    {
                        changeText("o");
                        break;
                    }
                case 14:
                    {
                        changeText("g");
                        break;
                    }
                case 15:
                    {
                        changeText("r");
                        break;
                    }
                case 16:
                    {
                        changeText("a");
                        break;
                    }
                case 17:
                    {
                        changeText("m");
                        break;
                    }
                case 18:
                    {
                        changeText("m");
                        break;
                    }
                case 19:
                    {
                        changeText("e");
                        break;
                    }
                case 20:
                    {
                        changeText("r");
                        break;
                    }
                case 21:
                    {
                        changeText("s");
                        break;
                    }
                case 22:
                    {
                        changeText(": ");
                        break;
                    }
                case 23:
                    {
                        changeText("\r\nMeysam ZarePour");
                        break;
                    }
                case 24:
                    {
                        changeText("\r\n...Hadi ZareZadeh");
                        break;
                    }
                case 25:
                    {
                        changeText("\r\n......MohammadReza HassanZadeh");
                        break;
                    }
                case 26:
                    {
                        changeText("\r\n\r\nSpecial Thanks To : ");
                        break;
                    }
                case 27:
                    {
                        changeText("Karim");
                        break;
                    }
                case 28:
                    {
                        changeText(" Heydari");
                        break;
                    }
                case 29:
                    {
                        changeText("\r\n\r\nVideo Club(Stack) Project. 1392-93. Enghelab Eslami Technical University Of Tehran.");
                        break;
                    }
                case 30:
                    {
                        changeText("\r\n\r\nGoodBye ...");
                        break;
                    }
                case 35:
                    {
                        exit = true;
                        changeText("\r\n\r\nPress Any Key To Exit...");
                        break;
                    }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            skin = comboBox1.Text;


            //age theme bezarim moqe resize kardane form error mide ! inja migim age 
            //theme gozashte boodim form ro kari kon ke resize nashe !
            //age theme nazashtim ya theme 0 bood kari kon qabele resize shodan bashe !

            if (skin != "0")
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            else
            {
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                Width = 818;
                Height = 501;
            }

            File.WriteAllText(Application.StartupPath + "\\skin.ini",skin);
            skinEngine1.SkinFile = @"skins\skin (" + skin + ").ssk";
            if (comboBox1.Text != "0")
                skinEngine1.Active = true;
            else
                skinEngine1.Active = false;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                textBox1.BackColor = colorDialog1.Color;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                textBox1.ForeColor = colorDialog1.Color;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowColor = true;
            fontDialog1.ShowEffects = true;
            fontDialog1.ShowHelp = true;
            if (fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                textBox1.Font = fontDialog1.Font;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "Text File(*.txt)|*.txt|All Types(*.*)|*.*";
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);
        }

        private void timerExit_Tick(object sender, EventArgs e)
        {
            Opacity -= 0.01; //kam kam shafafiate e form ro kamesh kon (az 0 ta 1 e) 
            if (Opacity == 0) //age 0 shod az barname kharej sho
                Application.Exit();
        }

        private void timerRun_Tick(object sender, EventArgs e)
        {
            Opacity += 0.01; //kam kam shafafiate e form ro ezafe kon (az 0 ta 1 e) 
            if (Opacity >= 1)//age 1 ya bishtar shod dige edame nade
                timerRun.Enabled = false;
        }
    }
}
