ThemedShapeStyles.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample demonstrates the available predefined themed shape styles
    // that are supported by DsWord.
    // We first generate a number of different shapes with varying fill
    // and line colors, then duplicate that shape, and apply a themed style
    // to the copy.
    public class ThemedShapeStyles
    {
        public GcWordDocument CreateDocx()
        {
            var styles = typeof(ThemedShapeStyle).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                .Where(p_ => p_.FieldType == typeof(ThemedShapeStyle));
            int stylesCount = styles.Count();

            var doc = new GcWordDocument();

            // We will apply each preset to 2 consecutive shapes:
            var shapes = AddGeometryTypes(doc, new SizeF(100, 100), stylesCount * 2, true, true);

            doc.Body.Paragraphs.Insert($"Themed Shape Styles ({stylesCount})", doc.Styles[BuiltInStyleId.Title], InsertLocation.Start);

            if (shapes.Count() > stylesCount * 2)
                shapes.Skip(stylesCount).ToList().ForEach(s_ => s_.Delete());

            int styleIdx = 0;
            int flop = 0;
            foreach (var s in shapes)
            {
                var shape = s.GetRange().CopyTo(s.GetRange(), InsertLocation.After).ParentObject as Shape;
                var style = styles.ElementAt((flop++ % 2 == 0) ? styleIdx : styleIdx++);
                // Apply the themed style to the shape:    
                shape.ApplyThemedStyle((ThemedShapeStyle)style.GetValue(null));
                // Insert the style's name in front of the styled shape:
                shape.GetRange().Runs.Insert($"{style.Name}:", InsertLocation.Before);
                shape.GetRange().Runs.Insert("\n", InsertLocation.After);
            }

            return doc;
        }

        /// <summary>
        /// Adds a paragraph with a single empty run, and adds a shape for each available GeometryType.
        /// The fill and line colors of the shapes are varied.
        /// </summary>
        /// <param name="doc">The target document.</param>
        /// <param name="size">The size of shapes to create.</param>
        /// <param name="count">The maximum number of shapes to create (-1 for no limit).</param>
        /// <param name="skipUnfillable">Add only shapes that support fills.</param>
        /// <param name="noNames">Do not add geometry names as shape text frames.</param>
        /// <returns>The list of shapes added to the document.</returns>
        private static List<Shape> AddGeometryTypes(GcWordDocument doc, SizeF size, int count = -1, bool skipUnfillable = false, bool noNames = false)
        {
            // Line and fill colors:
            Color[] lines = new Color[] { Color.Blue, Color.SlateBlue, Color.Navy, Color.Indigo, Color.BlueViolet, Color.CadetBlue, };
            int line = 0;
            Color[] fills = new Color[] { Color.MistyRose, Color.BurlyWood, Color.Coral, Color.Goldenrod, Color.Orchid, Color.Orange, Color.PaleVioletRed, };
            int fill = 0;

            // The supported geometry types:
            var geoms = Enum.GetValues(typeof(GeometryType));

            // Add a paragraph and a run where the shapes will live:
            doc.Body.Paragraphs.Add("");

            var shapes = new List<Shape>();
            foreach (GeometryType g in geoms)
            {
                // Line geometries do not support fills:
                if (skipUnfillable && g.IsLineGeometry())
                    continue;

                if (count-- == 0)
                    break;

                float w = size.Width, h = size.Height;
                var shape = doc.Body.Runs.Last.GetRange().Shapes.Add(w, h, g);
                if (!g.IsLineGeometry())
                {
                    shape.Fill.Type = FillType.Solid;
                    shape.Fill.SolidFill.RGB = fills[fill < fills.Length - 1 ? ++fill : (fill = 0)];
                }
                shape.Line.Width = 3;
                shape.Line.Fill.SolidFill.RGB = lines[line < lines.Length - 1 ? ++line : (line = 0)];
                if (!noNames && g.TextFrameSupported())
                    shape.AddTextFrame(g.ToString());
                shape.AlternativeText = $"This is shape {g}";
                shape.Size.EffectExtent.AllEdges = 8;
                shapes.Add(shape);
            }
            return shapes;
        }
    }
}