using Microsoft.VisualBasic;
using PdfFileWriter;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZX_Keyboard.Logic
{
    internal class RenderPdf
    {
        internal void CreateDocument()
        {
            var filename = "ZX-Keyboard.pdf";

            // Create empty document
            using (PdfDocument Document = new PdfDocument(PaperType.A4, false, UnitOfMeasure.mm, filename))
            {
                // Add new page
                PdfPage Page = new PdfPage(Document);

                // Add contents to page
                PdfContents Contents = new PdfContents(Page);

                // create font
                PdfFont ArialNormal = PdfFont.CreatePdfFont(Document, "Arial", FontStyle.Regular, true);
                PdfDrawTextCtrl TextCtrl = new PdfDrawTextCtrl(ArialNormal, 10.0);

                // draw text
                TextCtrl.Justify = TextJustify.Center;
                Contents.DrawText(TextCtrl, 304, 830, "ZX-Keyboard Created by Badmuts");

                for (int duplicate = 0; duplicate <= 4; duplicate++)
                {
                    var vMargin = -55 * duplicate;
                    // Row 1
                    Contents.AddNumberGraphButton(Document, 15, vMargin + 275, "1", 0b0100, Color.Blue, "!", "EDIT", "DEF FN");
                    Contents.AddNumberGraphButton(Document, 29, vMargin + 275, "2", 0b1000, Color.Red, "@", "CAPS LOCK", "FN");
                    Contents.AddNumberGraphButton(Document, 43, vMargin + 275, "3", 0b1100, Color.Magenta, "#", "TRUE VIDEO", "LINE");
                    Contents.AddNumberGraphButton(Document, 57, vMargin + 275, "4", 0b0010, Color.Green, "$", "INV VIDEO", "OPEN");
                    Contents.AddNumberGraphButton(Document, 71, vMargin + 275, "5", 0b0110, Color.Cyan, "%", "3", "CLOSE");
                    Contents.AddNumberGraphButton(Document, 85, vMargin + 275, "6", 0b1010, Color.Yellow, "&", "6", "MOVE");
                    Contents.AddNumberGraphButton(Document, 99, vMargin + 275, "7", 0b0001, Color.Black, "'", "5", "ERASE");
                    Contents.AddNumberGraphButton(Document, 113, vMargin + 275, "8", 0b0000, Color.Black, "(", "4", "POINT");
                    Contents.AddNumberButton(Document, 127, vMargin + 275, "9", ")", "GRAPHICS", "CAT");
                    Contents.AddNumberButton(Document, 141, vMargin + 275, "0", "_", "DELETE", "FORMAT");

                    // Row 2
                    Contents.AddButton(Document, 155, vMargin + 275, "Q", "PLOT", "<=", "SIN", "ASN");
                    Contents.AddButton(Document, 169, vMargin + 275, "W", "DRAW", "<>", "COS", "ACS");
                    Contents.AddButton(Document, 183, vMargin + 275, "E", "REM", ">=", "TAN", "ATN");
                    Contents.AddButton(Document, 15, vMargin + 262, "R", "RUN", "<", "INT", "VERIFY");
                    Contents.AddButton(Document, 29, vMargin + 262, "T", "RAND", ">", "RND", "MERGE");
                    Contents.AddButton(Document, 43, vMargin + 262, "Y", "RETURN", "AND", "STR$", "[");
                    Contents.AddButton(Document, 57, vMargin + 262, "U", "IF", "OR", "CHR$", "]");
                    Contents.AddButton(Document, 71, vMargin + 262, "I", "INPUT", "AT", "CODE", "IN");
                    Contents.AddButton(Document, 85, vMargin + 262, "O", "POKE", ";", "PEEK", "OUT");
                    Contents.AddButton(Document, 99, vMargin + 262, "P", "PRINT", "\"", "TAB", "©");

                    // Row 3
                    Contents.AddButton(Document, 113, vMargin + 262, "A", "NEW", "STOP", "READ", "~");
                    Contents.AddButton(Document, 127, vMargin + 262, "S", "SAVE", "NOT", "RESTORE", "|");
                    Contents.AddButton(Document, 141, vMargin + 262, "D", "DIM", "STEP", "DATA", "\\");
                    Contents.AddButton(Document, 155, vMargin + 262, "F", "FOR", "TO", "SGN", "{");
                    Contents.AddButton(Document, 169, vMargin + 262, "G", "GOTO", "THEN", "ABS", "}");
                    Contents.AddButton(Document, 183, vMargin + 262, "H", "GOSUB", "↑", "SQR", "CIRCLE");
                    Contents.AddButton(Document, 15, vMargin + 249, "J", "LOAD", "-", "VAL", "VAL$");
                    Contents.AddButton(Document, 29, vMargin + 249, "K", "LIST", "+", "LEN", "SCREEN$");
                    Contents.AddButton(Document, 43, vMargin + 249, "L", "LET", "=", "USR", "ATR");
                    Contents.AddTextButton(Document, 57, vMargin + 249, "ENTER", "", 7, Color.Black, Color.Red);

                    // Row 4
                    Contents.AddTextButton(Document, 71, vMargin + 249, "CAPS", "SHIFT", 5, Color.Black, Color.White);
                    Contents.AddButton(Document, 85, vMargin + 249, "Z", "COPY", ":", "LN", "BEEP");
                    Contents.AddButton(Document, 99, vMargin + 249, "X", "CLEAR", "£", "EXP", "INK");
                    Contents.AddButton(Document, 113, vMargin + 249, "C", "CONT", "?", "LPRINT", "PAPER");
                    Contents.AddButton(Document, 127, vMargin + 249, "V", "CLS", "/", "LLIST", "FLASH");
                    Contents.AddButton(Document, 141, vMargin + 249, "B", "BORDER", "*", "BIN", "BRIGHT");
                    Contents.AddButton(Document, 155, vMargin + 249, "N", "NEXT", ",", "INKEY$", "OVER");
                    Contents.AddButton(Document, 169, vMargin + 249, "M", "PAUSE", ".", "PI", "INVERSE");
                    Contents.AddTextButton(Document, 183, vMargin + 249, "SYMBOL", "SHIFT", 5, Color.Red, Color.White);
                    Contents.AddTextButton(Document, 15, vMargin + 236, "BREAK", "SPACE", 5, Color.Black, Color.White);

                    // Extra for colour labels
                    Contents.AddNumberGraphButton(Document, 29, vMargin + 236, "1", 0b0100, Color.Black, "!", "EDIT", "DEF FN");
                    Contents.AddNumberGraphButton(Document, 43, vMargin + 236, "2", 0b1000, Color.Black, "@", "CAPS LOCK", "FN");
                    Contents.AddNumberGraphButton(Document, 57, vMargin + 236, "3", 0b1100, Color.Black, "#", "TRUE VIDEO", "LINE");
                    Contents.AddNumberGraphButton(Document, 71, vMargin + 236, "4", 0b0010, Color.Black, "$", "INV VIDEO", "OPEN");
                    Contents.AddNumberGraphButton(Document, 85, vMargin + 236, "5", 0b0110, Color.Black, "%", "3", "CLOSE");
                    Contents.AddNumberGraphButton(Document, 99, vMargin + 236, "6", 0b1010, Color.Black, "&", "6", "MOVE");
                    Contents.AddNumberGraphButton(Document, 113, vMargin + 236, "7", 0b1110, Color.Black, "'", "5", "ERASE");
                    Contents.AddNumberGraphButton(Document, 127, vMargin + 236, "8", 0b0000, Color.Black, "(", "4", "POINT");

                    Contents.AddColourLabel(Document, 141, vMargin + 242, "BLUE", Color.Blue);
                    Contents.AddColourLabel(Document, 141, vMargin + 236, "RED", Color.Red);
                    Contents.AddColourLabel(Document, 155, vMargin + 242, "MAGENTA", Color.Magenta);
                    Contents.AddColourLabel(Document, 155, vMargin + 236, "GREEN", Color.Green);
                    Contents.AddColourLabel(Document, 169, vMargin + 242, "CYAN", Color.Cyan);
                    Contents.AddColourLabel(Document, 169, vMargin + 236, "YELLOW", Color.Yellow);
                    Contents.AddColourLabel(Document, 183, vMargin + 242, "WHITE", Color.White);
                }

                // create pdf file
                Document.CreateFile();

                // start default PDF reader and display the file
                Process Proc = new Process();
                Proc.StartInfo = new ProcessStartInfo(filename) { UseShellExecute = true };
                Proc.Start();

            }
        }
    }
}
