using System;
using System.Collections.Generic;
using Docs.Word;


namespace SampleManager
{
	struct Product
	{
		internal string	ProductName;
		internal int	CategoryIndex;
		internal bool	IsInStock;
	}

	class FormFields_Fill
	{
		internal static Document Main1()
		{
			//	It's an example of some database, you can use any database you need
			List<Product> Products = new List<Product>()
			{	
			    new Product() { ProductName = "Forrest Gump", CategoryIndex = 0, IsInStock = true },
			    new Product() { ProductName = "Harry Potter", CategoryIndex = 1, IsInStock = false },
			    new Product() { ProductName = "Call of Duty", CategoryIndex = 2, IsInStock = true },
			    new Product() { ProductName = "Titanic", CategoryIndex = 0, IsInStock = true },
			    new Product() { ProductName = "World Of Warcraft", CategoryIndex = 2, IsInStock = false }
			};

			//	Open the document with given name and get NodeList of first section
			Document Doc = Document.ReadDOC(Form1.SourceFolder + "FormFields - Fill.doc");
			NodeList Nodes = Doc.Sections[0].Nodes;

			//	For all products in our database...
			for (int i = 0; i < 5; i++)
			{
				//	Get next paragraph
				Paragraph Par = (Paragraph)Nodes[i+1];

				//	Search all textruns of this paragraph
				for (int j = 0; j < Par.Textruns.Count; j++)
				{
					//	If this textrun is of FormField type...
					if (Par.Textruns[j].Type == TextrunType.FormField)
					{
						FormField FF = Par.Textruns[j].Fields.FormField;
						FF.Textruns.Clear();

						//	If it's textbox - insert Products[i].ProductName in it
						if (FF.Type == FormFieldType.Textbox)
							FF.Textruns.Add(new Textrun(Products[i].ProductName));

						//	If it's a droplist
						//	Change its current selected item to value of Products[i].CategoryIndex
						if (FF.Type == FormFieldType.DropDown)
							FF.DropDown.CurrentItemIndex = Products[i].CategoryIndex;
						
						//	If it's a checkbox
						//	Change its current state to value of Products[i].IsInStock
						if (FF.Type == FormFieldType.Checkbox)
							FF.CheckBox.Checked = Products[i].IsInStock;
					}
				}
			}

			return Doc;
		}
	}
}
