﻿// <copyright file="SPCmdletAddListItem.cs" company="Ignyte BVBA">
// Copyright (c) Ignyte BVBA. All rights reserved.
// </copyright>

namespace Ignyte.SharePoint.PowerShell.Cmdlets.Lists
{
    using System.Collections;
    using System.Management.Automation;
    using Ignyte.SharePoint.PowerShell.Cmdlets.Base.Pipebinds;
    using Microsoft.SharePoint;

    [Cmdlet(VerbsCommon.New, "SPListItem", SupportsShouldProcess = true)]
    public class SPCmdletAddListItem : SPNewCmdletBaseCustom<SPListItem>
    {
        [Parameter(
            Mandatory = true,
            Position = 1,
            ParameterSetName = ParameterAttribute.AllParameterSets)]
        public string Title { get; set; }

        [Parameter(
            Mandatory = true,
            ParameterSetName = ParameterAttribute.AllParameterSets)]
        public SPListPipeBind List { get; set; }

        [Parameter(
            Mandatory = false)]
        public PropertiesBind Values;

        [Parameter(
            Mandatory = false)]
        public SwitchParameter Overwrite { get; set; }

        protected override SPListItem CreateDataObject()
        {
            Hashtable fieldValues = null;
            if (this.Values != null)
            {
                fieldValues = this.Values.Read();
            }

            SPList list = null;
            if (this.List != null)
            {
                list = this.List.Read();
            }

            if (list != null)
            {
                SPListItem newItem = list.AddItem();
                if (fieldValues != null)
                {
                    foreach (var key in fieldValues.Keys)
                    {
                        SPField field = newItem.Fields.GetField(key as string);
                        switch (field.TypeAsString)
                        {
                            case "User":
                            case "UserMulti":
                                {
                                    var userValue = new SPFieldUserValueCollection();
                                    var value = fieldValues[key];
                                    if (value.GetType().IsArray)
                                    {
                                        foreach (var arrayItem in value as object[])
                                        {
                                            int userid;
                                            if (!int.TryParse(arrayItem as string, out userid))
                                            {
                                                var user = list.ParentWeb.EnsureUser(arrayItem as string);
                                                userValue.Add(new SPFieldUserValue(list.ParentWeb, user.ID, user.LoginName));
                                            }
                                            else
                                            {
                                                userValue.Add(new SPFieldUserValue() { LookupId = userid });
                                            }
                                        }

                                        newItem[key as string] = userValue;
                                    }
                                    else
                                    {
                                        var user = list.ParentWeb.EnsureUser(value as string);
                                        newItem[key as string] = user;
                                    }

                                    newItem.Update();
                                    break;
                                }

                            case "TaxonomyFieldType":
                            case "TaxonomyFieldTypeMulti":
                                {
                                    var value = fieldValues[key];
                                    if (value.GetType().IsArray)
                                    {
                                    }
                                    else
                                    {
                                    }

                                    break;
                                }

                            default:
                                {
                                    newItem[key as string] = fieldValues[key];
                                    newItem.Update();
                                    break;
                                }
                        }
                    }
                }

                return newItem;
            }
            else
            {
                return null;
            }
        }
    }
}
