/*********************************** END OF VARIABLE DEFINITIONS ***********************************************************************/


      
        public SetupAssistant()
        {
            InitializeComponent();

            configuration = Program.getConfiguration();
            capture_stream = new CaptureStream();
            capture = Program.Capture;


            // Default: Team 1 is down
            team1_up = false;

            textBox_savelocation.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + textBox_gamename.Text;
        }
        /*********************************** END OF VARIABLE DEFINITIONS ***********************************************************************/



        public SetupAssistant()
        {
            InitializeComponent();

            configuration  = Program.getConfiguration();
            capture_stream = new CaptureStream();
            capture        = Program.Capture;


            // Default: Team 1 is down
            team1_up = false;

            textBox_savelocation.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + textBox_gamename.Text;
        }
        /************************************* END OF VARIABLE DEFINITIONS ********************************************************/





        public Form_Main(XmlOutput xmlDoc)
        {
            InitializeComponent();

            // Load default configuration
            configuration = Program.getConfiguration();
            applyConfiguration(configuration);


            newgame = false;


            this.game = new Game(configuration.Teama, configuration.Teamb);


            // add the first set
            this.game.Sets.Add(new Set(1));

            capture_stream = new CaptureStream();
            capture = Program.Capture;
           
            list_timestamps = new List<long>();
            imageList_screenshots = new ImageList();
            imageList_screenshots.ImageSize = new Size(84, 68);

            codec = Emgu.CV.CvInvoke.CV_FOURCC('P', 'I', 'M', '1');

            this.StartPosition = FormStartPosition.Manual;
            this.DesktopLocation = new Point(0, 0);

            this.xmlDoc = xmlDoc;

            xmlDoc.setGame(this.game);
        }
        public void loadRallies(Form_Main mainForm)
        {
            XmlNode rootNode;
            rootNode = xmlDoc.DocumentElement;

            List<long> list_timestamps = new List<long>();
            ImageList imageList_screenshots = new ImageList();
            imageList_screenshots.ImageSize = new Size(84, 68);

            mainForm.listView_screenshots.Items.Clear();


            List<Set> sets = new List<Set>();
            sets.Add(new Set(1));
            sets.Add(new Set(2));
            sets.Add(new Set(3));

            XmlNodeList rallies = xmlDoc.GetElementsByTagName("Spielzug");

            for (int i = 0; i < rallies.Count; i++)
            {
                int setnr = Convert.ToInt32(rallies[i].SelectSingleNode("@Satz").InnerText);
                long starttime = Convert.ToInt64(rallies[i].SelectSingleNode("@ID").InnerText);
                list_timestamps.Add(starttime);
                

                Rally rally = new Rally(starttime,setnr);


                rally.Kill = (rallies[i].SelectSingleNode("@Erfolg").InnerText == "True");
                rally.Smash = (rallies[i].SelectSingleNode("@Smash").InnerText == "True");
                rally.Drop = (rallies[i].SelectSingleNode("@Drop").InnerText == "True");
                rally.BigPoint = (rallies[i].SelectSingleNode("@Bigpoint").InnerText == "True");
                rally.Timeout = (rallies[i].SelectSingleNode("@Timeout").InnerText == "True");
                rally.Standardposition = (rallies[i].SelectSingleNode("@Standardseite").InnerText == "True");
 

                // Get All Positions
                XmlNode xmlposition = rallies[i].SelectSingleNode("Aufschlag");
                rally.Service_pos = getPosition(xmlposition);

                xmlposition = rallies[i].SelectSingleNode("Annahme");
                rally.Reception_pos = getPosition(xmlposition);

                rally.Reception_pos.Player = new Person();
                String playername = rallies[i].SelectSingleNode("ReceptionPlayer/Player/@Name").InnerText;
                rally.Reception_pos.Player = readPlayer(playername);


                XmlNodeList defenceplayers = rallies[i].SelectNodes("Punktchance_aus_Defence/Player");
                List<Person> defenceplayerslist = new List<Person>();
                for (int c = 0; c < defenceplayers.Count; c++)
                {
                    string name = defenceplayers[c].SelectSingleNode("@Name").InnerText;
                    defenceplayerslist.Add(readPlayer(name));
                }
                rally.Reception_pos.Defenceplayers = defenceplayerslist;



                xmlposition = rallies[i].SelectSingleNode("Zuspiel");
                rally.Set_pos = getPosition(xmlposition);

                xmlposition = rallies[i].SelectSingleNode("Anlauf");
                rally.Takeoff_pos = getPosition(xmlposition);

                xmlposition = rallies[i].SelectSingleNode("Angriff");
                rally.Approach_pos = getPosition(xmlposition);

                xmlposition = rallies[i].SelectSingleNode("Abwehr");
                rally.Defence_pos = getPosition(xmlposition);



                // Set Screenshot
                CaptureStream capture_stream = new CaptureStream();
                Bitmap screenshot = capture_stream.createScreenshot(starttime, false);
                if (screenshot != null)
                {

                    imageList_screenshots.Images.Add(screenshot);
                    mainForm.setImageListScreenshots(imageList_screenshots);

                    int screenshot_index = i;
                    ListViewItem screenshot_item = new ListViewItem("", screenshot_index);

                    mainForm.listView_screenshots.Items.Add(screenshot_item);
                    mainForm.listView_screenshots.LargeImageList = mainForm.getImageListScreenshots();
                    mainForm.listView_screenshots.Refresh();
                }


                

                sets[setnr - 1].Rallies.Add(rally);
                
            }

            mainForm.Game.Sets = sets;
            mainForm.List_timestamps = list_timestamps;
        }
        public void loadRallies(Form_Main mainForm)
        {
            XmlNode rootNode;

            rootNode = xmlDoc.DocumentElement;

            List <long> list_timestamps       = new List <long>();
            ImageList   imageList_screenshots = new ImageList();

            imageList_screenshots.ImageSize = new Size(84, 68);

            mainForm.listView_screenshots.Items.Clear();


            List <Set> sets = new List <Set>();

            sets.Add(new Set(1));
            sets.Add(new Set(2));
            sets.Add(new Set(3));

            XmlNodeList rallies = xmlDoc.GetElementsByTagName("Spielzug");

            for (int i = 0; i < rallies.Count; i++)
            {
                int  setnr     = Convert.ToInt32(rallies[i].SelectSingleNode("@Satz").InnerText);
                long starttime = Convert.ToInt64(rallies[i].SelectSingleNode("@ID").InnerText);
                list_timestamps.Add(starttime);


                Rally rally = new Rally(starttime, setnr);


                rally.Kill             = (rallies[i].SelectSingleNode("@Erfolg").InnerText == "True");
                rally.Smash            = (rallies[i].SelectSingleNode("@Smash").InnerText == "True");
                rally.Drop             = (rallies[i].SelectSingleNode("@Drop").InnerText == "True");
                rally.BigPoint         = (rallies[i].SelectSingleNode("@Bigpoint").InnerText == "True");
                rally.Timeout          = (rallies[i].SelectSingleNode("@Timeout").InnerText == "True");
                rally.Standardposition = (rallies[i].SelectSingleNode("@Standardseite").InnerText == "True");


                // Get All Positions
                XmlNode xmlposition = rallies[i].SelectSingleNode("Aufschlag");
                rally.Service_pos = getPosition(xmlposition);

                xmlposition         = rallies[i].SelectSingleNode("Annahme");
                rally.Reception_pos = getPosition(xmlposition);

                rally.Reception_pos.Player = new Person();
                String playername = rallies[i].SelectSingleNode("ReceptionPlayer/Player/@Name").InnerText;
                rally.Reception_pos.Player = readPlayer(playername);


                XmlNodeList   defenceplayers     = rallies[i].SelectNodes("Punktchance_aus_Defence/Player");
                List <Person> defenceplayerslist = new List <Person>();
                for (int c = 0; c < defenceplayers.Count; c++)
                {
                    string name = defenceplayers[c].SelectSingleNode("@Name").InnerText;
                    defenceplayerslist.Add(readPlayer(name));
                }
                rally.Reception_pos.Defenceplayers = defenceplayerslist;



                xmlposition   = rallies[i].SelectSingleNode("Zuspiel");
                rally.Set_pos = getPosition(xmlposition);

                xmlposition       = rallies[i].SelectSingleNode("Anlauf");
                rally.Takeoff_pos = getPosition(xmlposition);

                xmlposition        = rallies[i].SelectSingleNode("Angriff");
                rally.Approach_pos = getPosition(xmlposition);

                xmlposition       = rallies[i].SelectSingleNode("Abwehr");
                rally.Defence_pos = getPosition(xmlposition);



                // Set Screenshot
                CaptureStream capture_stream = new CaptureStream();
                Bitmap        screenshot     = capture_stream.createScreenshot(starttime, false);
                if (screenshot != null)
                {
                    imageList_screenshots.Images.Add(screenshot);
                    mainForm.setImageListScreenshots(imageList_screenshots);

                    int          screenshot_index = i;
                    ListViewItem screenshot_item  = new ListViewItem("", screenshot_index);

                    mainForm.listView_screenshots.Items.Add(screenshot_item);
                    mainForm.listView_screenshots.LargeImageList = mainForm.getImageListScreenshots();
                    mainForm.listView_screenshots.Refresh();
                }



                sets[setnr - 1].Rallies.Add(rally);
            }

            mainForm.Game.Sets       = sets;
            mainForm.List_timestamps = list_timestamps;
        }