using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Winbooks.Apis.Services;

namespace Winbooks.Apis.Demo
{
    public class GetDocumentsHttp
    {
        private static UserCredential _userCredential;

        public static string MainExecute(string url, UserCredential userCredential)
        {
            _userCredential = userCredential;
            string respons = userCredential.PostAsync(userCredential.CreateHttpClient(), url + "/app/DocumentDetails/Folder/BV2", GetCriteria("INV", 20190025));
            if (respons == null)
                return "Null";
            Newtonsoft.Json.Linq.JArray details = Newtonsoft.Json.Linq.JArray.Parse(respons);
            if (details == null)
                return "None";
            Test.LogTable(new string[] { "Number", "Date", "Reference", "ItemCode", "Name", "UP", "Disc", "Qty", "Amount" }, new int[] { 10, 15, 20, 20, 30, 10, 6, 6, 10 }, details);
            Tools.Log(" => Done");

            return "OK";
        }

        private static string GetCriteria(string jnl, int lastNumber)
        {
            StringBuilder s = new StringBuilder(2000);

            s.Append(@" {""EntityType"":""Winbooks.TORM.OM.Logistics.DocumentDetail, Winbooks.TORM.OM"", ");
            s.Append(@" ""DAOType"":""Winbooks.TORM.DAL.Logistics.DocumentDetailDAO, Winbooks.TORM.DAL"", ");
            s.Append(@" ""IsCustomization"":false, ");
            s.Append(@" ""Alias"":""this"", ");
            s.Append(@" ""Association"":{ ");
            s.Append(@"  ""header"":{ ""AliasName"":""header"",""Type"":""Winbooks.TORM.OM.Logistics.DocumentHeader, Winbooks.TORM.OM"",""JoinType"":1,""LeftProperty"":""Id"",""RightProperty"":""DocumentHeader_Id""}, ");
            s.Append(@"  ""row"":{ ""AliasName"":""row"",""Type"":""Winbooks.TORM.OM.Logistics.DocumentRow, Winbooks.TORM.OM"",""JoinType"":1,""LeftProperty"":""Id"",""RightProperty"":""DocumentRow_Id""}, ");
            s.Append(@"  ""item"":{ ""OwnerAlias"":""row"",""AliasName"":""item"",""Type"":""Winbooks.TORM.OM.Logistics.Item, Winbooks.TORM.OM"",""JoinType"":1,""LeftProperty"":""Id"",""RightProperty"":""Item_Id""}, ");
            s.Append(@"  ""jnl"":{ ""OwnerAlias"":""header"",""AliasName"":""jnl"",""Type"":""Winbooks.TORM.OM.Logistics.JournalLog, Winbooks.TORM.OM"",""JoinType"":1,""LeftProperty"":""Id"",""RightProperty"":""JournalLog_Id""}}, ");
            s.Append(@"  ""Conditions"":[{""Operator"":0,""PropertyName"":""jnl.Code"",""OtherPropertyName"":"""",""Values"":[""" + jnl + @"""],""ValueTypes"":[""System.String, mscorlib""]}, ");
            s.Append(@" {""Operator"":5,""PropertyName"":""header.Number"",""OtherPropertyName"":"""",""Values"":[" + lastNumber.ToString() + @"],""ValueTypes"":[""System.Int32, mscorlib""]}], ");
            s.Append(@" ""Havings"":[], ");
            s.Append(@" ""ProjectionsList"":[ ");
            s.Append(@"  {""PropertyName"":""header.Number"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""header.Date"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""header.Reference"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""item.Code"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""row.Name"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""row.UnitPrice"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""row.DiscountRate"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""this.Quantity"",""Operator"":22}, ");
            s.Append(@"  {""PropertyName"":""this.Amount"",""Operator"":22}], ");
            s.Append(@" ""Orders"":[],""Params"":{},""FirstResult"":-1,""MaxResult"":-1,""ResultState"":0,""TimeOutSeconds"":0} ");

            return s.ToString();
        }
    }
}
