DataTplUseCases.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.Generic;
using System.Text;
using System.Data;
using System.Linq;
using System.Globalization;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // The code in this example loads a DOCX document (created in MS Word)
    // containing a report template, and depending on the selected demo,
    // either adds a data source and generates the data bound report,
    // or simply returns the unmodified template for reference.
    public class DataTplUseCases
    {
        public GcWordDocument CreateDocx(string[] sampleParams)
        {
            var doc = new GcWordDocument();

            // Load the template DOCX:
            doc.Load(Path.Combine("Resources", "WordDocs", sampleParams[3]));

            using var ds = new DataSet();
            // Load the data set:
            ds.ReadXml(Path.Combine("Resources", "data", "DsWordTplDataSet.xml"));

            // Return the unmodified template document for reference:
            if (sampleParams.Length >= 6 && sampleParams[5] == "template")
                return doc;

            // Add the data source to the data template data sources:
            if (sampleParams.Length >= 5 && !string.IsNullOrEmpty(sampleParams[4]))
                doc.DataTemplate.DataSources.Add("ds", ds.Tables[sampleParams[4]]);
            else
                doc.DataTemplate.DataSources.Add("ds", ds);

            // The document already has all the necessary bindings,
            // so we only need to process the data template:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Done:
            return doc;
        }

        public GcWordDocument CreateDocx(int parsIdx = 0)
        {
            return CreateDocx(GetSampleParamsList()[parsIdx]);
        }

        public static List<string[]> GetSampleParamsList()
        {
            // Mandatory: name, description, info
            // Custom: template DOCX [[, table] , "template"]
            return new List<string[]>()
            {
                new string[] { "@use-data-tpl/Lease Agreement", "Generate a lease agreement", null,
                    "Lease_Agreement_Template.docx", "LeaseAgreement" },
                new string[] { "@use-data-tpl/Consulting Agreement", "Generate a consultancy agreement", null,
                    "Consulting_Agreement_Template.docx", "ConsAgreement" },
                new string[] { "@use-data-tpl/Rental Agreement", "Generate a house rental agreement", null,
                    "House_Rental_Template.docx", "HouseRentalAgreement" },
                new string[] { "@use-data-tpl/Employment Contract", "Generate an employment contract", null,
                    "Employment_Contract_Template.docx", "EmploymentContract" },

                // Parameter sets below simply return the template used to produce the bound document in corresponding
                // data template samples:
                new string[] { "@use-data-tpl-src/Lease Agreement", "Template Lease_Agreement_Template.docx", null,
                    "Lease_Agreement_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Consulting Agreement", "Template Consulting_Agreement_Template.docx", null,
                    "Consulting_Agreement_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Rental Agreement", "Template House_Rental_Template.docx", null,
                    "House_Rental_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Employment Contract", "Template Employment_Contract_Template.docx", null,
                    "Employment_Contract_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Order Invoice", "Template InvoiceTemplate.docx", null,
                    "InvoiceTemplate.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Closing Disclosure", "Template Closing_Disclosure_Template.docx", null,
                    "Closing_Disclosure_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Vacation Itinerary", "Template Vacation_Itinerary_Template.docx", null,
                    "Vacation_Itinerary_Template.docx", null, "template" },
            };
        }
    }
}