using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
namespace Scratch
{
        public partial class Form1 : Form
    {
        Bitmap objBmpImage = new Bitmap(1, 1);
        Bitmap objBmpImage2 = new Bitmap(1, 1);
        Bitmap workingImage = new Bitmap(1, 1);
        Bitmap mainImage = new Bitmap(1, 1);
        Bitmap winningNumberImage = new Bitmap(1, 1);
        Bitmap winningTextImage = new Bitmap(1, 1);
        Bitmap bmp1 = new Bitmap(420, 630);
        Bitmap bmp2 = new Bitmap(420, 630);
        int intWidth = 60;
        int intHeight = 36;
        Random rnd = new Random();
        int x = 135;
        int y = 260;
        int size = 30;
        int textsize = 10;
        Bitmap pictureBox1Image;
        Bitmap pictureBox2Image;
        Bitmap pictureBox3Image;
        Bitmap pictureBox4Image = new Bitmap(global::Scratch.Properties.Resources.Blank1);
        
        string[] playersWinNumNumbers = new string[15];
        string[] drawNumNumbers = new string[15];
        // no need for: string[] prizeIndexIntegers
        string[] prizeAmounts = new string[15];
      
        int[] playersWinNumIntegers = new int[3];
        int[] drawNumIntegers = new int[15];
        int[] prizeIndexIntegers = new int[15];
        int[] prizeAmountIntegers = new int[15];
            
        int totalAmountOfWinningMatches = 0;
        string totalAmountOfWinningMatchesText = "0";
        int totalInPrizes = 0;
        string totalInPrizesText = "0";
        int currentTicketPrice = 1;
        string currentTicketPriceText = "1";
        
        string[] myData = new string[] { "1", "ONE", "1", "$1.00", "2", "TWO", "2", "$2.00", "3", "THREE", "3", "$3.00", "4", "FOUR", "4", "$4.00", "5", "FIVE", "5", "$5.00", "6", "SIX", "6", "$6.00", "7", "SEVEN", "7", "$7.00", "8", "EIGHT", "8", "$8.00", "9", "NINE", "9", "$9.00", "10", "TEN", "10", "$10.00", "11", "ELEVN", "11", "$11.00", "12", "TWLVE", "12", "$12.00", "13", "THRTN", "13", "$13.00", "14", "FORTN", "14", "$14.00", "15", "FIVTN", "15", "$15.00", "16", "SIXTN", "16", "$16.00", "17", "SEVTN", "17", "$17.00", "18", "EGHTN", "18", "$18.00", "19", "NINTN", "19", "$19.00", "20", "TWNTY", "20", "$20.00", "21", "TWONE", "21", "$21.00", "22", "TWTWO", "22", "$22.00", "23", "TWTHR", "23", "$23.00", "24", "TWFOR", "24", "$24.00", "25", "TWFIV", "25", "$25.00", "26", "TWSIX", "26", "$26.00", "27", "TWSEV", "27", "$27.00", "28", "TWEGT", "28", "$28.00", "29", "TWNIN", "29", "$29.00", "30", "THRTY", "30", "$30.00", "31", "TRTONE", "31", "$31.00", "32", "TRTTWO", "32", "$32.00", "33", "TRTTHR", "33", "$33.00", "34", "TRTFOR", "34", "$34.00", "35", "TRTFIV", "35", "$35.00", "36", "TRTSIX", "36", "$36.00", "37", "TRTSEV", "37", "$37.00", "38", "TRTEGT", "38", "$38.00", "39", "TRTNIN", "39", "$39.00", "40", "FORTY", "40", "$40.00", "41", "ONEHUN", "100", "$100.00", "42", "TWOHUN", "200", "$200.00", "43", "FIFTY", "50", "$50.00", "44", "SEVFIV", "75", "$75.00", "45", "ONEFTY", "150", "$150.00", "46", "THRHUN", "300", "$300.00", "47", "FORHUN", "400", "$400.00", "48", "FIVHUN", "500", "$500.00", "49", "SIXHUN", "600", "$600.00", "50", "SEVHUN", "700", "$700.00", "51", "EGTHUN", "800", "$800.00", "52", "NINHUN", "900", "$900.00", "53", "ONETHO", "1000", "$1,000.00", "54", "TWOTHO", "2000", "$2,000.00", "55", "TRETHO", "3000", "$3,000.00", "56", "FORTHO", "4000", "$4,000.00", "57", "FIVTHO", "5000", "$5,000.00", "58", "SIXTHO", "6000", "$6,000.00", "59", "SEVTHO", "7000", "$7,000.00", "60", "EGTTHO", "8000", "$8,000.00", "61", "NINETHO", "9000", "$9,000.00", "62", "TENTHO", "10000", "$10,000.00", "63", "ELEVTHO", "11000", "$11,000.00", "64", "TWLVTHO", "12000", "$12,000.00", "65", "THRTNTHO", "13000", "$13,000.00", "66", "FRTNTHO", "14000", "$14,000.00", "67", "FVTNTHO", "15000", "$15,000.00", "68", "SXTNTHO", "16000", "$16,000.00", "69", "SVTNTHO", "17000", "$17,000.00", "70", "EGTNTHO", "18000", "$18,000.00", "71", "NINTNTHO", "19000", "$19,000.00", "72", "TWTYTHO", "20000", "$20,000.00", "73", "TYFVTHO", "25000", "$25,000.00", "74", "THRTTHO", "30000", "$30,000.00", "75", "TRTFVTHO", "40000", "$40,000.00", "76", "FRTYTHO", "45000", "$45,000.00", "77", "FIFTYTHO", "50000", "$50,000.00", "78", "FFTFVTO", "55000", "$55,000.00", "79", "SXTYTHO", "60000", "$60,000.00", "80", "ONEHNTHO", "100000", "$100,000.00", "81", "TWHUNTHO", "200000", "$200,000.00", "82", "TRHUNTHO", "300000", "$300,000.00", "83", "FRHNDTHO", "400000", "$400,000.00", "84", "FVHUNTHO", "500000", "$500,000.00", "85", "SXHNTHO", "600000", "$600,000.00", "86", "SVNHNTHO", "700000", "$700,000.00", "87", "EGTHUNTHO", "800000", "$800,000.00", "88", "NINHUNTHO", "900000", "$900,000.00", "89", "ONEMILN", "1000000", "$1,000,000.00" };
        int totalx = 0;
        string[] myPrizeData = new string[] { "1", "ONE", "1", "$1.00", "2", "TWO", "2", "$2.00", "3", "FIVE", "5", "$5.00", "4", "TEN", "10", "$10.00", "5", "TWNTY", "20", "$20.00", "6", "TWFIV", "25", "$25.00", "7", "ONEHUN", "100", "$100.00", "8", "FIVHUN", "500", "$500.00", "9", "ONETHO", "1000", "$1,000", "10", "FIVTHO", "5000", "$5,000", "11", "TENTHO", "10000", "$10,000", "12", "FVTNTHO", "15000", "$15,000", "13", "TYFVTHO", "25000", "$25,000" };
        int yy = 0;
        int ff= 0;
        int ww= 0;
        // CONCLUDES GLOBAL VARIABLES +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        
        public Form1()
        {
            InitializeComponent();
        }
        private Bitmap CreatePlayersWinNumNumbers(string nImageText, int size)
        {
            webBrowser1.Document.BackColor = Color.DarkGray;
            currentTicketPriceText = Convert.ToString(currentTicketPrice);
        // Create the Font object for the image text drawing.
            Font objFont = new Font("Perpetua", size, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
            Graphics objGraphics = Graphics.FromImage(objBmpImage);
        // Create the bmpImage again with the correct size for the text and font.
            objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
        // Add the colors to the new bitmap.
            objGraphics = Graphics.FromImage(objBmpImage);
        // Set Background color
            objGraphics.Clear(Color.White);
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            objGraphics.DrawString(nImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
            objGraphics.Flush();
        return (objBmpImage);
        }
        private Bitmap CreatePlayersWinNumText(string tImageText, int textsz)
        {
            Font objFontsmall = new Font("Arial", textsz, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
            Graphics objGraphics2 = Graphics.FromImage(objBmpImage2);
            objBmpImage2 = new Bitmap(objBmpImage2, new Size(intWidth, intHeight));
            objGraphics2 = Graphics.FromImage(objBmpImage2);
            objGraphics2.Clear(Color.White);
            objGraphics2.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics2.TextRenderingHint = TextRenderingHint.AntiAlias;
            objGraphics2.DrawString(tImageText, objFontsmall, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
            objGraphics2.Flush();
        return (objBmpImage2);
        }
        private Bitmap mergeImages(Bitmap source1, Bitmap source2, int nx, int ny, int tx, int ty)
        {
        var target = new Bitmap(source1.Width, source1.Height, PixelFormat.Format32bppArgb);
        var graphics = Graphics.FromImage(target);
            graphics.CompositingMode = CompositingMode.SourceOver; // this is the default, but just to be clear
            graphics.DrawImage(source1, nx, ny);
            graphics.DrawImage(source2, tx, ty);
        return target;
        }
        private Bitmap mergeIntoPictureBox(Bitmap source1, Bitmap source2, int x, int y)
        {
        var target = new Bitmap(source1.Width, source1.Height, PixelFormat.Format32bppArgb);
        var graphics = Graphics.FromImage(target);
            graphics.CompositingMode = CompositingMode.SourceOver; // this is the default, but just to be clear
            graphics.DrawImage(source1, 0, 0);
            graphics.DrawImage(source2, x, y);
        return target;
        }
        private string getNumberText(int i)
        {
        int l = (i * 4) + 1;
        return myData[l];
        }
        private string getNumberCurrency(int i)
        {
        int l = (i * 4) + 3;
        return myData[l];
        }
        private string getNumberNumeral(int i)
        {
        int l = (i * 4) + 2;
        return myData[l];
        }
        private string getPrizeText(int i)
        {
        int l = (i * 4) + 1;
        return myPrizeData[l];
        }
        private string getPrizeCurrency(int i)
        {
        int l = (i * 4) + 3;
        if (myPrizeData[l].Equals("$10,000") || myPrizeData[l].Equals("$25,000") || myPrizeData[l].Equals("$50,000"))
            {
                size = 16;
                x = x - 4;
            }
        return myPrizeData[l];
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            x = 135;
        }
        private void button2_Click(object sender, EventArgs e)
        {
        }
        private void startNow()
        {
        int locationNumeralX = 0;//10
        int locationNumeralY = 0;
        int locationTextX = 0;//5
        int locationTextY = 27;//27
        int getNewRandom;
        int getNewPrizeRandom;
        string wnns = "";
        // 3 PLAYER NUMBERS
        // CREATE THE THREE PLAYER NUMBERS ONTO THE TICKET
        for (int c = 1; c <= 3; c++)
            {
                getNewRandom = rnd.Next(1, 41);
                playersWinNumIntegers.SetValue(getNewRandom, 0);// 1?
        string wnn = getNumberNumeral(getNewRandom); wnns = wnn;
        string wnt = getNumberText(getNewRandom);
                size = 30;
                textsize = 10;
                locationTextY = 27;
                locationTextX = 0;
                winningNumberImage = CreatePlayersWinNumNumbers(wnn, size);
                winningTextImage = CreatePlayersWinNumText(wnt, textsize);
                pictureBox1Image = winningNumberImage;
                pictureBox2Image = winningTextImage;
                mainImage = new Bitmap(pictureBox1Image);
                workingImage = new Bitmap(pictureBox2Image);
                pictureBox3Image = (Bitmap)mergeImages(mainImage, workingImage, locationNumeralX, locationNumeralY, locationTextX, locationTextY);
                bmp1 = new Bitmap(pictureBox4Image);
                bmp2 = new Bitmap(pictureBox3Image);
                pictureBox4Image = mergeIntoPictureBox(bmp1, bmp2, x, y);
                textBox2.Text = textBox2.Text + wnns + " ";//THE ONLY HOLDER FOR THE 3 PLAYER NUMBERS
                x = x + 60;
            }
        //change start position
            x = 85;
            y = 300;
            textBox1.Text = "dunno";
            
        // NUMBERS DRAW AREA
        // CREATE THE REMAINING 15 NUMBERS ONTO THE TICKET
        for (int c = 0; c < 3; c++)//3 represents rows
            {
        for (int h = 0; h < 5; h++)//five represents how many images to produce (5 in 3 rows)
                {
                    getNewRandom = rnd.Next(1, 41);
                    drawNumIntegers.SetValue(getNewRandom, 1);
        // CHECK TO SEE IF THE LAST DRAW IS A WINNERS
                    checkDrawIntegerForWinner(getNewRandom);
        string wnn = getNumberNumeral(getNewRandom);
        string wnt = getNumberText(getNewRandom);
                    size = 30;
                    textsize = 10;
                    locationTextY = 27;
                    locationTextX = 0;
                    winningNumberImage = CreatePlayersWinNumNumbers(wnn, size);
                    winningTextImage = CreatePlayersWinNumText(wnt, textsize);
                    pictureBox1Image = winningNumberImage;
                    pictureBox2Image = winningTextImage;
                    mainImage = new Bitmap(pictureBox1Image);
                    workingImage = new Bitmap(pictureBox2Image);
                    pictureBox3Image = (Bitmap)mergeImages(mainImage, workingImage, locationNumeralX, locationNumeralY, locationTextX, locationTextY);
                    bmp1 = new Bitmap(pictureBox4Image);
                    bmp2 = new Bitmap(pictureBox3Image);
                    pictureBox4Image = new Bitmap(mergeIntoPictureBox(bmp1, bmp2, x, y));
                    x = x + 60;
                }
                x = 75;
                y = y + 39;
        // WITHIN CREATE (ABOVE) CREATE MATCHING PRIZE AMOUNTS ONTO THE TICKET
        for (int h = 0; h < 5; h++)//five represents how many images to produce
                {
                    getNewPrizeRandom = rnd.Next(1, 13);//prize amounts are INDIVIDUALLY random and are without control 
                    prizeAmountIntegers.SetValue(getNewPrizeRandom, 1);
        string wnc = getPrizeCurrency(getNewPrizeRandom); richTextBox1.Text = richTextBox1.Text + getPrizeCurrency(getNewPrizeRandom) + "\n\r";//just testing with added code
        string wnt2 = getPrizeText(getNewPrizeRandom);
                    size = 18;
                    textsize = 7;
                    locationTextY = 17;
                    locationTextX = 17;
                    winningNumberImage = CreatePlayersWinNumNumbers(wnc, size);
                    winningTextImage = CreatePlayersWinNumText(wnt2, textsize);
                    pictureBox1Image = winningNumberImage;
                    pictureBox2Image = winningTextImage;
                    mainImage = new Bitmap(pictureBox1Image);
                    workingImage = new Bitmap(pictureBox2Image);
                    pictureBox3Image = (Bitmap)mergeImages(mainImage, workingImage, locationNumeralX, locationNumeralY, locationTextX, locationTextY);
                    bmp1 = new Bitmap(pictureBox4Image);
                    bmp2 = new Bitmap(pictureBox3Image);
                    pictureBox4Image = new Bitmap(mergeIntoPictureBox(bmp1, bmp2, x, y));
                    x = x + 62;
                }
                x = 85;
                y = y + 26;
            }
        //see if any drawn match winner. get prize values for those matching
           
            
        //for (int chc = 0; chc < 3; chc++)
        //{
        //    for (int chcr = 0; chcr < 2; chcr++)
        //    {
        //        tTB1.Text = tTB1.Text + Convert.ToString(getPlayersWinNumIntegerArrayValue(chcr)) + " ";
        //        tTB2.Text = tTB2.Text + Convert.ToString(getDrawnIntegerArrayValue(chc)) + " ";
        //        //System.Threading.Thread.Sleep(3000);
        //        if (getPlayersWinNumIntegerArrayValue(chcr) == getDrawnIntegerArrayValue(chc))
        //        {
        //            totalAmountOfWinningMatches.SetValue(getPlayersWinNumIntegerArrayValue(chcr), chcr);
        //            .SetValue(getMatchingPrizeInt(chcr), chcr);
        //            textBox4.Text = Convert.ToString([chcr]);
        //        }
        //    }
        //}
        
        getTotalInPrizesForTicket();
        }
        public void getTotalInPrizesForTicket()
        {
        for(int zx=0; zx < totalAmountOfWinningMatches; zx++)
            {
        int tu = playersWinNumIntegers[zx];
        for (int gs=0; gs < 15; gs++){
        if (tu == drawNumIntegers[gs] )
                    {
                        totalInPrizes = totalInPrizes + getPrizeDollarValue(gs);
                        tTB1.Text = Convert.ToString(totalInPrizes);
                    }
                }
                
                
                
            }
        }
        public void checkDrawIntegerForWinner(int drawnNumber)
        {
        for (int pz = 0; pz < 3; pz ++)
            {
        //how many are there
        if (pz <= playersWinNumIntegers.Length - 1)
                { 
        if (drawnNumber == playersWinNumIntegers[pz])
                    {
                        totalAmountOfWinningMatches ++;
                textBox5.Text = Convert.ToString(totalAmountOfWinningMatches);
                    }
                }
            }
       }
        public int getPlayersWinNumIntegerArrayValue(int index)
        {
           
        if(index <= playersWinNumIntegers.Length - 1)//not to exceed bounds)
            { 
            ww = playersWinNumIntegers[index];
            }
        return ww;
        }
        public int getDrawnIntegerArrayValue(int index)
        {
        if(index <= drawNumIntegers.Length - 1)//not to exceed bounds)
            { 
            ff = drawNumIntegers[index];
            }
        return ff;
         }
        public int getMatchingPrizeInt(int index)
        {
        if (index <= prizeAmountIntegers.Length - 1)//not to exceed bounds
            {  
                yy = prizeAmountIntegers[index];
            }
        return yy;
        }
        public void tallyPrizeAmounts()
        {
        for(int qq = 0;qq < totalAmountOfWinningMatches; qq++)
            {
                
                totalInPrizes = totalInPrizes + getPrizeDollarValue(qq);
            }
           
        }
        public int getPrizeDollarValue(int index)
        {
            textBox6.Text = Convert.ToString(index);
            textBox7.Text =  myPrizeData[index +2];
            
            
        int pdd = 0;
        string gover ="";
            gover = myPrizeData[index +2];
        return pdd;
        }
        private void beginImageMaking(object sender, MouseEventArgs e)
        {
            startNow();
            pictureBox4Image.Save("ScratcherNumber.png", ImageFormat.Png);
            webBrowser1.Visible = true;
            webBrowser1.Url = new Uri("H:/Scratch/Scratch/TicketCreation/Scratch.swf");
            webBrowser1.Update();
            x = 135;
            y = 260;
        }
        private void beginImageMaking()
        {
            startNow();
            pictureBox4Image.Save("ScratcherNumber.png", ImageFormat.Png);
            webBrowser1.Visible = true;
            webBrowser1.Url = new Uri("H:/Scratch/Scratch/TicketCreation/Scratch.swf");
            webBrowser1.Update();
            textBox1.Text = "began imagemaking";
            x = 135;
            y = 260;
        }
        public void switchTickets(FileInfo cf)
        {
        string newCoverFileName = cf.ToString();
        string originalCoverFileName = "Cover.png";
        string sourcePath = @"H:\Scratch\Scratch\TicketCreation\";
        string targetPath = @"H:\Scratch\Scratch\TicketCreation\";
        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath, newCoverFileName);
        string destFile = System.IO.Path.Combine(targetPath, originalCoverFileName);
        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);
            beginImageMaking();
        }
        private void pictureBox5_Click(object sender, EventArgs e)
        {
            groupBox4.BackColor = Color.HotPink;
            groupBox3.BackColor = Color.DarkGray;
            groupBox2.BackColor = Color.DarkGray;
            groupBox1.BackColor = Color.DarkGray;
            currentTicketPrice = 10;
            FileInfo cf = new FileInfo("Cover5.png");
            switchTickets(cf);
        }
        private void pictureBox4_Click_1(object sender, EventArgs e)
        {
            groupBox4.BackColor = Color.DarkGray;
            groupBox3.BackColor = Color.HotPink;
            groupBox2.BackColor = Color.DarkGray;
            groupBox1.BackColor = Color.DarkGray;
            currentTicketPrice = 5;
            FileInfo cf = new FileInfo("Cover4.png");
            switchTickets(cf);
        }
        private void pictureBox3_Click_1(object sender, EventArgs e)
        {
            groupBox4.BackColor = Color.DarkGray;
            groupBox3.BackColor = Color.DarkGray;
            groupBox2.BackColor = Color.HotPink;
            groupBox1.BackColor = Color.DarkGray;
            currentTicketPrice = 2;
            FileInfo cf = new FileInfo("Cover3.png");
            switchTickets(cf);
        }
        private void pictureBox2_Click_1(object sender, EventArgs e)
        {
            groupBox4.BackColor = Color.DarkGray;
            groupBox3.BackColor = Color.DarkGray;
            groupBox2.BackColor = Color.DarkGray;
            groupBox1.BackColor = Color.HotPink;
            currentTicketPrice = 1;
            FileInfo cf = new FileInfo("Cover2.png");
            switchTickets(cf);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            beginImageMaking();
        }
    }
}