using PdfFileWriter;

namespace ZX_Keyboard.Logic
{
    static class PdfContentsExtention
    {
        public static void AddNumberGraphButton(this PdfContents contents,
            PdfDocument document,
            Double left,
            Double bottom,
            string key,
            byte graph,
            Color color,
            string cShifted,
            string eKeyword,
            string eShifted)
        {
            contents.AddNumberButton(document, left, bottom, key, cShifted, eKeyword, eShifted);

            // Draw graph
            // Outline
            PdfRectangle GraphRect = new PdfRectangle(left + 7.6, bottom + 6, left + 7.6 + 2, bottom + 6 + 2);
            PdfDrawCtrl DrawGraphCtrl = new PdfDrawCtrl();
            DrawGraphCtrl.Shape = DrawShape.Rectangle;
            DrawGraphCtrl.Paint = DrawPaint.Border;
            DrawGraphCtrl.BorderWidth = 0.1;
            DrawGraphCtrl.BorderColor = color;
            contents.DrawGraphics(DrawGraphCtrl, GraphRect);

            // Top Left
            if ((graph & 0b1000) != 0)
            {
                PdfRectangle innerRect = new PdfRectangle(left + 7.6, bottom + 7, left + 7.6 + 1, bottom + 7 + 1);
                PdfDrawCtrl DrawInnerCtrl = new PdfDrawCtrl();
                DrawInnerCtrl.Shape = DrawShape.Rectangle;
                DrawInnerCtrl.Paint = DrawPaint.Fill;
                DrawInnerCtrl.BackgroundTexture = color;
                contents.DrawGraphics(DrawInnerCtrl, innerRect);
            }

            // Top Right
            if ((graph & 0b0100) != 0)
            {
                PdfRectangle innerRect = new PdfRectangle(left + 8.6, bottom + 7, left + 8.6 + 1, bottom + 7 + 1);
                PdfDrawCtrl DrawInnerCtrl = new PdfDrawCtrl();
                DrawInnerCtrl.Shape = DrawShape.Rectangle;
                DrawInnerCtrl.Paint = DrawPaint.Fill;
                DrawInnerCtrl.BackgroundTexture = color;
                contents.DrawGraphics(DrawInnerCtrl, innerRect);
            }

            // Bottom Right
            if ((graph & 0b0010) != 0)
            {
                PdfRectangle innerRect = new PdfRectangle(left + 8.6, bottom + 6, left + 8.6 + 1, bottom + 6 + 1);
                PdfDrawCtrl DrawInnerCtrl = new PdfDrawCtrl();
                DrawInnerCtrl.Shape = DrawShape.Rectangle;
                DrawInnerCtrl.Paint = DrawPaint.Fill;
                DrawInnerCtrl.BackgroundTexture = color;
                contents.DrawGraphics(DrawInnerCtrl, innerRect);
            }

            // Bottom Left
            if ((graph & 0b0001) != 0)
            {
                PdfRectangle innerRect = new PdfRectangle(left + 7.6, bottom + 6, left + 7.6 + 1, bottom + 6 + 1);
                PdfDrawCtrl DrawInnerCtrl = new PdfDrawCtrl();
                DrawInnerCtrl.Shape = DrawShape.Rectangle;
                DrawInnerCtrl.Paint = DrawPaint.Fill;
                DrawInnerCtrl.BackgroundTexture = color;
                contents.DrawGraphics(DrawInnerCtrl, innerRect);
            }
        }

        public static void AddNumberButton(this PdfContents contents,
            PdfDocument document,
            Double left,
            Double bottom,
            string key,
            string cShifted,
            string eKeyword,
            string eShifted)
        {
            KeyOutline(contents, left, bottom);
            Key(contents, document, left, bottom, key);

            PdfDrawTextCtrl cShiftedTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 6);
            cShiftedTextCtrl.TextColor = Color.Red;
            cShiftedTextCtrl.Justify = TextJustify.Right;
            contents.DrawText(cShiftedTextCtrl, left + 8, bottom + 4, cShifted);

            PdfDrawTextCtrl eKeywordTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 5);
            if ("5678".Contains(key))
            {
                eKeywordTextCtrl = new PdfDrawTextCtrl(document, "Webdings", FontStyle.Bold, 8);
            }
            eKeywordTextCtrl.TextColor = Color.Black;
            eKeywordTextCtrl.Justify = TextJustify.Center;
            contents.DrawText(eKeywordTextCtrl, left + 6.3, bottom + 8.5, eKeyword);

            PdfDrawTextCtrl eShiftedTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 5);
            eShiftedTextCtrl.TextColor = Color.Red;
            eShiftedTextCtrl.Justify = TextJustify.Center;
            contents.DrawText(eShiftedTextCtrl, left + 6, bottom + 1, eShifted);

        }

        public static void AddTextButton(this PdfContents contents,
                PdfDocument document,
                Double left,
                Double bottom,
                string line1,
                string line2,
                int textSize,
                Color textColor,
                Color backColor)
        {
            Double width = 12.6;
            Double height = 11;
            PdfRectangle Rect = new PdfRectangle(left, bottom, left + width, bottom + height);
            PdfDrawCtrl DrawCtrl = new PdfDrawCtrl();
            DrawCtrl.Shape = DrawShape.RoundedRect;
            DrawCtrl.Paint = DrawPaint.BorderAndFill;
            DrawCtrl.BorderWidth = 0.1;
            DrawCtrl.BorderColor = Color.Black;
            DrawCtrl.BackgroundTexture = backColor;
            DrawCtrl.Radius = 1;
            contents.DrawGraphics(DrawCtrl, Rect);

            var line1vPos = bottom + 5;
            if (!string.IsNullOrEmpty(line2))
            {
                line1vPos = bottom + 6;
            }
            PdfDrawTextCtrl keyLine1Ctrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, textSize);
            keyLine1Ctrl.TextColor = textColor;
            keyLine1Ctrl.Justify = TextJustify.Center;
            contents.DrawText(keyLine1Ctrl, left + 6.3, line1vPos, line1);
            contents.DrawText(keyLine1Ctrl, left + 6.3, line1vPos - 2.5, line2);
        }

        public static void AddButton(this PdfContents contents,
                PdfDocument document,
                Double left,
                Double bottom,
                string key,
                string statement,
                string sShifted,
                string eKeyword,
                string eShifted)
        {
            KeyOutline(contents, left, bottom);
            Key(contents, document, left, bottom, key);
            Statement(contents, document, left, bottom, statement);

            PdfDrawTextCtrl sShiftedTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 6);
            sShiftedTextCtrl.TextColor = Color.Red;
            sShiftedTextCtrl.Justify = TextJustify.Right;
            contents.DrawText(sShiftedTextCtrl, left + 11.4, bottom + 6, sShifted);

            PdfDrawTextCtrl eKeywordTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 5);
            eKeywordTextCtrl.TextColor = Color.Green;
            eKeywordTextCtrl.Justify = TextJustify.Center;
            contents.DrawText(eKeywordTextCtrl, left + 6.3, bottom + 8.5, eKeyword);

            PdfDrawTextCtrl eShiftedTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 5);
            eShiftedTextCtrl.TextColor = Color.Red;
            eShiftedTextCtrl.Justify = TextJustify.Center;
            contents.DrawText(eShiftedTextCtrl, left + 6, bottom + 1, eShifted);
        }

        public static void AddColourLabel(this PdfContents contents,
        PdfDocument document,
        Double left,
        Double bottom,
        string text,
        Color colour)
        {
            Double width = 11.6;
            Double height = 5;
            PdfRectangle Rect = new PdfRectangle(left, bottom, left + width, bottom + height);
            PdfDrawCtrl DrawCtrl = new PdfDrawCtrl();
            DrawCtrl.Shape = DrawShape.RoundedRect;
            DrawCtrl.Paint = DrawPaint.Border;
            DrawCtrl.BorderColor = Color.Black;
            if (colour == Color.White || colour == Color.Yellow || colour == Color.Cyan)
            {
                DrawCtrl.Paint = DrawPaint.BorderAndFill;
                DrawCtrl.BackgroundTexture = Color.Black;
            }
            DrawCtrl.BorderWidth = 0.1;
            DrawCtrl.Radius = 1;
            contents.DrawGraphics(DrawCtrl, Rect);

            PdfDrawTextCtrl textCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 4);
            textCtrl.TextColor = colour;
            textCtrl.Justify = TextJustify.Center;
            contents.DrawText(textCtrl, left + 5.8, bottom + 2, text);

        }

        private static void KeyOutline(PdfContents contents, double left, double bottom)
        {
            Double width = 12.6;
            Double height = 11;
            PdfRectangle Rect = new PdfRectangle(left, bottom, left + width, bottom + height);
            PdfDrawCtrl DrawCtrl = new PdfDrawCtrl();
            DrawCtrl.Shape = DrawShape.RoundedRect;
            DrawCtrl.Paint = DrawPaint.Border;
            DrawCtrl.BorderWidth = 0.1;
            DrawCtrl.BorderColor = Color.Black;
            DrawCtrl.Radius = 1;
            contents.DrawGraphics(DrawCtrl, Rect);
        }

        private static void Key(PdfContents contents, PdfDocument document, double left, double bottom, string key)
        {
            PdfDrawTextCtrl keyTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 10);
            keyTextCtrl.TextColor = Color.Black;
            contents.DrawText(keyTextCtrl, left + 0.8, bottom + 4, key);
        }

        private static void Statement(PdfContents contents, PdfDocument document, double left, double bottom, string statement)
        {
            PdfDrawTextCtrl statementTextCtrl = new PdfDrawTextCtrl(document, "Verdana", FontStyle.Bold, 5);
            statementTextCtrl.TextColor = Color.Black;
            statementTextCtrl.Justify = TextJustify.Right;
            contents.DrawText(statementTextCtrl, left + 11.8, bottom + 4, statement);
        }
    }
}
