using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;

namespace raven_integration
{

    public class UserOptionsRu
    {

        /// <summary>
        /// Test all CRUD routes for a UserOptions object
        /// </summary>       
        [Fact]
        public async void RU()
        {

            //CREATE a user
            dynamic D1 = new JObject();
            D1.name = Util.Uniquify("Test UserOptions User");
            D1.ownerId = 1L;
            D1.active = true;
            D1.login = Util.Uniquify("LOGIN");
            D1.password = Util.Uniquify("PASSWORD");
            D1.roles = 0;//norole
            D1.localeId = 1;//random locale
            D1.userType = 3;//non scheduleable

            ApiResponse R = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D1.ToString());
            Util.ValidateDataReturnResponseOk(R);
            long UserId = R.ObjectResponse["data"]["id"].Value<long>();

            //Now there should be a user options available for this user


            //RETRIEVE companion USEROPTIONS object

            //Get it
            R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateDataReturnResponseOk(R);
            //ensure the default value is set
            R.ObjectResponse["data"]["uiColor"].Value<int>().Should().Be(0);
            uint concurrencyToken = R.ObjectResponse["data"]["concurrencyToken"].Value<uint>();

            //UPDATE
            //PUT
            dynamic D2 = new JObject();
            D2.emailaddress = "testuseroptions@helloayanova.com";
            D2.TimeZoneOffset = -7.5M;//Decimal value
            D2.UiColor = -2097216;//Int value (no suffix for int literals)
            D2.concurrencyToken = concurrencyToken;
            ApiResponse PUTTestResponse = await Util.PutAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"), D2.ToString());
            Util.ValidateHTTPStatusCode(PUTTestResponse, 200);

            //VALIDATE
            R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateDataReturnResponseOk(R);
            //ensure the default value is set
            R.ObjectResponse["data"]["emailAddress"].Value<string>().Should().Be(D2.emailaddress.ToString());
            R.ObjectResponse["data"]["timeZoneOffset"].Value<decimal>().Should().Be((decimal)D2.TimeZoneOffset);
            R.ObjectResponse["data"]["uiColor"].Value<int>().Should().Be((int)D2.UiColor);
            concurrencyToken = R.ObjectResponse["data"]["concurrencyToken"].Value<uint>();


            //PATCH  
            string newEmail = "patchtestuseroptions@helloayanova.com";
            string patchJson = "[{\"value\": \"" + newEmail + "\",\"path\": \"/emailAddress\",\"op\": \"replace\"}]";
            ApiResponse PATCHTestResponse = await Util.PatchAsync("UserOptions/" + UserId.ToString() + "/" + concurrencyToken.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"), patchJson);
            Util.ValidateHTTPStatusCode(PATCHTestResponse, 200);

            //check PATCH worked
            R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateDataReturnResponseOk(R);
            //ensure the default value is set
            R.ObjectResponse["data"]["emailAddress"].Value<string>().Should().Be(newEmail);
            R.ObjectResponse["data"]["timeZoneOffset"].Value<decimal>().Should().Be((decimal)D2.TimeZoneOffset);
            R.ObjectResponse["data"]["uiColor"].Value<int>().Should().Be((int)D2.UiColor);
            // concurrencyToken = R.ObjectResponse["data"]["concurrencyToken"].Value<uint>();

            //DELETE USER
            ApiResponse DELETETestResponse = await Util.DeleteAsync("User/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateHTTPStatusCode(DELETETestResponse, 204);

            //CHECK DELETE USER REMOVED USEROPTIONS
            R = await Util.GetAsync("UserOptions/" + UserId.ToString(), await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateResponseNotFound(R);
        }


        /// <summary>
        /// Test not found
        /// </summary>       
        [Fact]
        public async void GetNonExistentItemShouldError()
        {
            //Get non existant
            //Should return status code 404, api error code 2010
            ApiResponse R = await Util.GetAsync("UserOptions/999999", await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateResponseNotFound(R);
        }

        /// <summary>
        /// Test bad modelstate
        /// </summary>       
        [Fact]
        public async void GetBadModelStateShouldError()
        {
            //Get non existant
            //Should return status code 400, api error code 2200 and a first target in details of "id"
            ApiResponse R = await Util.GetAsync("UserOptions/2q2", await Util.GetTokenAsync("manager", "l3tm3in"));
            Util.ValidateBadModelStateResponse(R, "id");
        }










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

    }//eoc
}//eons
