using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using PlayingWithXmlInAspMvc.Models;

namespace PlayingWithXmlInAspMvc.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            XElement linqMyNodes = XElement.Load(Request.Url.AbsoluteUri + "PlayingWithXml.xml");
            IEnumerable<XElement> myLinqNodes = from myNodes in linqMyNodes.Elements("myNode") select myNodes;
            PlayingWithXmlModel[] myModel = new PlayingWithXmlModel[myLinqNodes.Count()];
            int i = 0;
            foreach (var myLinqNode in myLinqNodes)
            {
                myModel[i] = new PlayingWithXmlModel();
                var Configurations = from configuration in myLinqNode.Elements("Workflow").Elements("Configuration").Elements("ConfigItem")
                                     select configuration;

                foreach (var Configuration in Configurations)
                {
                    if (Configuration.Element("Key") != null)
                    {
                        switch (Configuration.Element("Key").Value)
                        {
                            case "ReportSuccessToAddress":
                                myModel[i].ReportSuccessToAddress = Configuration.Element("Value").Value.Replace(" ", string.Empty);
                                break;
                            case "ReportErrorToAddress":
                                myModel[i].ReportErrorToAddress = Configuration.Element("Value").Value.Replace(" ", string.Empty);
                                break;
                            case "ReportSuccessCcAddress":
                                myModel[i].ReportSuccessCcAddress = Configuration.Element("Value").Value.Replace(" ", string.Empty);
                                break;
                        }
                    }
                }
                if (myLinqNode.Element("Name") != null)
                {
                    myModel[i].Name = myLinqNode.Element("Name").Value;
                }
                i++;
            }
            return View(myModel);
        }
    }
}