using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace raven_integration
{

    public class Locale
    {

        /*

         ImportLocale(ct, ResourceFolderPath, "en"); - id 1
                    ImportLocale(ct, ResourceFolderPath, "es"); - id 2
                    ImportLocale(ct, ResourceFolderPath, "fr"); - id 3
                    ImportLocale(ct, ResourceFolderPath, "de"); - id 4
         */

        [Fact]
        public async void LocalePickListWorks()
        {
            //Get all
            ApiResponse a = await Util.GetAsync("Locale/picklist", await Util.GetTokenAsync("ClientLimited"));//lowest level test user because there are no limits on this route except to be authenticated
            Util.ValidateDataReturnResponseOk(a);
            Util.ValidateHTTPStatusCode(a, 200);
            //there should be at least 4 of them as there are 4 stock locales
            ((JArray)a.ObjectResponse["data"]).Count.Should().BeGreaterThan(3);
        }


        [Fact]
        public async void GetFullLocaleWorks()
        {
            //Get all
            ApiResponse a = await Util.GetAsync("Locale/1", await Util.GetTokenAsync("ClientLimited"));//lowest level test user because there are no limits on this route except to be authenticated
            Util.ValidateDataReturnResponseOk(a);
            Util.ValidateHTTPStatusCode(a, 200);
            //there should be dozens of keys but at times there might only be a few during development so at least verify there is more than one
            ((JArray)a.ObjectResponse["data"]["localeItems"]).Count.Should().BeGreaterThan(0);
        }


        [Fact]
        public async void GetSubsetWorks()
        {
            /*
            {
                "localeId": 0,
                "keys": [
                    "string"
                ]
            }
             */

            List<string> keys = new List<string>();
            keys.AddRange(new string[] { "AddressType", "ClientName", "RateName", "WorkorderService" });
            dynamic d = new JObject();
            //d.localeId = 1;
            //d.keys = JToken.FromObject(keys);
            d = JToken.FromObject(keys);

            ApiResponse a = await Util.PostAsync("Locale/subset", await Util.GetTokenAsync("ClientLimited"), d.ToString());
            Util.ValidateDataReturnResponseOk(a);
            Util.ValidateHTTPStatusCode(a, 200);
            //there should be dozens of keys but at times there might only be a few during development so at least verify there is more than one
            ((JArray)a.ObjectResponse["data"]).Count.Should().Be(4);
        }


        [Fact]
        public async void DuplicateUpdateAndDeleteWorks()
        {
            /*
            {
                "id": 1,
                "name": "CustomTest1"
            }
             */

            //DUPLICATE
            dynamic d = new JObject();
            d.id = 1;
            d.name = Util.Uniquify("INTEGRATION-TEST-LOCALE");

            ApiResponse a = await Util.PostAsync("Locale/Duplicate", await Util.GetTokenAsync("BizAdminFull"), d.ToString());
            Util.ValidateDataReturnResponseOk(a);
            Util.ValidateHTTPStatusCode(a, 201);
            //verify the object returned is as expected
            a.ObjectResponse["data"]["name"].Value<string>().Should().Be(d.name.ToString());
            a.ObjectResponse["data"]["stock"].Value<bool>().Should().Be(false);
            a.ObjectResponse["data"]["id"].Value<long>().Should().BeGreaterThan(4);
            a.ObjectResponse["data"]["concurrencyToken"].Value<uint>().Should().BeGreaterThan(0);
            ((JArray)a.ObjectResponse["data"]["localeItems"]).Count.Should().BeGreaterThan(0);

            long NewId = a.ObjectResponse["data"]["id"].Value<long>();

            //UPDATE
            //Update locale name

            /*
            
            {
                "id": 10,
                "newText": "What the hell?",
                "concurrencyToken": 25174
            }

             */
            dynamic d2 = new JObject();
            d2.id = NewId;
            d2.newText = Util.Uniquify("INTEGRATION-TEST-LOCALE NAME UPDATE");
            d2.concurrencyToken = a.ObjectResponse["data"]["concurrencyToken"].Value<uint>();
            ApiResponse PUTTestResponse = await Util.PutAsync("Locale/UpdateLocaleName", await Util.GetTokenAsync("BizAdminFull"), d2.ToString());
            Util.ValidateHTTPStatusCode(PUTTestResponse, 200);


            ApiResponse checkPUTWorked = await Util.GetAsync("Locale/" + NewId.ToString(), await Util.GetTokenAsync("BizAdminFull"));
            Util.ValidateNoErrorInResponse(checkPUTWorked);
            checkPUTWorked.ObjectResponse["data"]["name"].Value<string>().Should().Be(d2.newText.ToString());
            //uint concurrencyToken = PUTTestResponse.ObjectResponse["data"]["concurrencyToken"].Value<uint>();


            //Update locale key
            var FirstLocaleKey = ((JArray)a.ObjectResponse["data"]["localeItems"])[0];
            long UpdatedLocaleKeyId = FirstLocaleKey["id"].Value<long>();
            d2.id = UpdatedLocaleKeyId;
            d2.newText = Util.Uniquify("INTEGRATION-TEST-LOCALEITEM DISPLAY UPDATE");
            d2.concurrencyToken = FirstLocaleKey["concurrencyToken"].Value<uint>();

            string UpdatedLocaleKey = FirstLocaleKey["key"].Value<string>();

            PUTTestResponse = await Util.PutAsync("Locale/UpdateLocaleItemDisplayText", await Util.GetTokenAsync("BizAdminFull"), d2.ToString());
            Util.ValidateHTTPStatusCode(PUTTestResponse, 200);

            //create user that is set to new locale so can use getSubset            
            var Login = Util.Uniquify("LOGIN");
            var Password = Util.Uniquify("PASSWORD");
            dynamic DUSER = new JObject();
            DUSER.name = Util.Uniquify("LocaleUpdateSubsetTestUser");
            DUSER.ownerId = 1L;
            DUSER.active = true;
            DUSER.login = Login;
            DUSER.password = Password;
            DUSER.roles = 0;//norole (any role can get a subset of locale keys)
            DUSER.localeId = NewId;//random locale
            DUSER.userType = 3;//non scheduleable
            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), DUSER.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long DUSERID = a.ObjectResponse["data"]["id"].Value<long>();



            List<string> keys = new List<string>();
            keys.AddRange(new string[] { UpdatedLocaleKey });
            dynamic d3 = new JObject();
            //d3.localeId = NewId;
            d3 = JToken.FromObject(keys);

            checkPUTWorked = await Util.PostAsync("Locale/subset", await Util.GetTokenAsync(Login, Password), d3.ToString());
            Util.ValidateDataReturnResponseOk(checkPUTWorked);
            Util.ValidateHTTPStatusCode(checkPUTWorked, 200);
            ((JArray)checkPUTWorked.ObjectResponse["data"]).Count.Should().Be(1);
            var FirstLocaleKeyUpdated = ((JArray)checkPUTWorked.ObjectResponse["data"])[0];

            FirstLocaleKeyUpdated["value"].Value<string>().Should().Be(d2.newText.ToString());

            //DELETE TEMPORARY USER SO CAN DELETE LOCALE
            a = await Util.DeleteAsync("User/" + DUSERID.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateHTTPStatusCode(a, 204);

            //DELETE TEMP LOCALE
            a = await Util.DeleteAsync("Locale/" + NewId.ToString(), await Util.GetTokenAsync("BizAdminFull"));
            Util.ValidateHTTPStatusCode(a, 204);


        }



        //==================================================

    }//eoc
}//eons
