OddEvenRows.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Layout;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // This example shows how to draw a table with differently shaded odd and even rows,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class OddEvenRows
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using var g = bmp.CreateGraphics(Color.White);
            g.Transform = Matrix3x2.CreateTranslation(-pixelSize.Width / 2, 0) * Matrix3x2.CreateScale(2);
            DrawTable(g, pixelSize.Width, pixelSize.Height);
            g.Transform = Matrix3x2.Identity;
            return bmp;
        }

        // Data source for the table:
        static readonly Team[] _teams = new Team[]
        {
            new Team("England"),
            new Team("France"),
            new Team("Ireland"),
            new Team("Italy"),
            new Team("Scotland"),
            new Team("Wales"),
        };

        static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
        {
            var host = new LayoutHost();
            var view = host.CreateView(pageWidth, pageHeight);

            var rt = view.CreateRect();
            rt.AnchorTopRight(null, 36, 36);

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopRight,
                rowCount: _teams.Length + 2,
                columnCount: 11,
                gridLineColor: Color.FromArgb(173, 223, 252),
                gridLineWidth: 1,
                rowMinHeight: 10,
                columnMinWidth: 10);

            var fmt = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "tahoma.ttf")),
                FontSize = 18,
                FontSizeInGraphicUnits = true
            };
            var cs = new CellStyle
            {
                TextFormat = fmt,
                FixedWidth = false,
                PaddingAll = 4
            };
            var csHeaderH = new CellStyle(cs)
            {
                TextFormat = new TextFormat(fmt)
                {
                    ForeColor = Color.White,
                    FontBold = true
                },
                FillColor = Color.FromArgb(17, 93, 140),
                TextAlignment = TextAlignment.Center,
                ParagraphAlignment = ParagraphAlignment.Center,
            };
            var csHeaderV = new CellStyle(csHeaderH)
            {
                RotationAngle = 270,
                TextAlignment = TextAlignment.Leading,
                PaddingLeft = 3
            };
            var csNumber = new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Center
            };
            var csNation = new CellStyle(cs)
            {
                TextFormat = new TextFormat(fmt)
                {
                    ForeColor = Color.FromArgb(50, 123, 197)
                },
            };

            // add cells to the header
            ta.AddCell(csHeaderV, 0, 0, 2, 1, "Position");
            ta.AddCell(csHeaderH, 0, 1, 2, 1, "Nation");
            ta.AddCell(csHeaderH, 0, 2, 1, 4, "Games");
            ta.AddCell(csHeaderV, 1, 2, "Played");
            ta.AddCell(csHeaderV, 1, 3, "Won");
            ta.AddCell(csHeaderV, 1, 4, "Drawn");
            ta.AddCell(csHeaderV, 1, 5, "Lost");
            ta.AddCell(csHeaderH, 0, 6, 1, 4, "Points");
            ta.AddCell(csHeaderV, 1, 6, "For");
            ta.AddCell(csHeaderV, 1, 7, "Against");
            ta.AddCell(csHeaderV, 1, 8, "Difference");
            ta.AddCell(csHeaderV, 1, 9, "Tries");
            ta.AddCell(csHeaderH, 0, 10, 2, 1, "Table\npoints");

            // add the data cells
            for (int i = 0; i < _teams.Length; i++)
            {
                int rowIndex = i + 2;
                var team = _teams[i];
                ta.AddCell(csNumber, rowIndex, 0, $"{i + 1}");
                ta.AddCell(csNation, rowIndex, 1, team.Nation);
                ta.AddCell(csNumber, rowIndex, 2, $"{team.Played}");
                ta.AddCell(csNumber, rowIndex, 3, $"{team.Won}");
                ta.AddCell(csNumber, rowIndex, 4, $"{team.Drawn}");
                ta.AddCell(csNumber, rowIndex, 5, $"{team.Lost}");
                ta.AddCell(csNumber, rowIndex, 6, $"{team.For}");
                ta.AddCell(csNumber, rowIndex, 7, $"{team.Against}");
                ta.AddCell(csNumber, rowIndex, 8, $"{team.Diff}");
                ta.AddCell(csNumber, rowIndex, 9, $"{team.Tries}");
                ta.AddCell(csNumber, rowIndex, 10, $"{team.TablePoints}");
            }

            // change background for odd rows
            ta.DefaultCellStyle = new CellStyle
            {
                Background = true,
                FillColor = Color.FromArgb(238, 238, 238)
            };
            for (int i = 0; i < _teams.Length; i += 2)
            {
                ta.AddCell(i + 2, 0, 1, 11);
            }

            ta.Render();
        }

        // A class to store example data:
        class Team
        {
            static readonly Random _rnd = Common.Util.NewRandom();

            public string Nation;
            public int Played, Won, Drawn, Lost;
            public int For, Against, Diff, Tries;
            public int TablePoints;

            internal Team(string nation)
            {
                Nation = nation;
                Played = _rnd.Next(0, 50);
                Won = _rnd.Next(0, 50);
                Drawn = _rnd.Next(0, 50);
                Lost = _rnd.Next(0, 50);
                For = _rnd.Next(0, 50);
                Against = _rnd.Next(0, 50);
                Diff = _rnd.Next(0, 50);
                Tries = _rnd.Next(0, 50);
                TablePoints = _rnd.Next(0, 150);
            }
        }
    }
}