﻿using CmsData.Codes;
using IntegrationTests.Support;
using SharedTestFixtures;
using Shouldly;
using Xunit;

namespace IntegrationTests.Areas.Search.Views.Query
{
    [Collection(Collections.Webapp)]
    public class ResultsTests : AccountTestBase
    {
        [Fact]
        public void Should_Update_Tag()
        {
            username = RandomString();
            password = RandomString();
            var user = CreateUser(username, password, roles: new[] { "Access", "Edit", "Admin" });
            Login();

            MaximizeWindow();

            Open($"{rootUrl}NewQuery");

            Wait(10);
            Find(css: "#QueryConditionSelect .close > span").Click();

            Find(id: "CancelChange").Click();
            Find(id: "Run").Click();

            Wait(10);
            WaitForElement(css: ".btn > .fa-cog");
            Find(css: ".btn > .fa-cog").Click();
            Find(xpath: "//a[contains(text(),'Add All')]").Click();


            Wait(3);
            Find(id: "tagname").Clear();
            Find(id: "tagname").SendKeys("NewTagShouldBeUpdated");
            Find(css: ".modal-footer > .btn-primary").Click();

            Wait(10);
            driver.PageSource.ShouldContain("NewTagShouldBeUpdated");
        }
        [Fact]
        public void Should_Skip_Disabled_Conditions()
        {
            username = RandomString();
            password = RandomString();
            var user = CreateUser(username, password, roles: new[] { "Access", "Edit", "Admin" });
            var kid1 = CreatePerson(family: user.Person.Family, positionInFamilyId: PositionInFamily.Child, genderId: GenderCode.Male, age: 6);
            var kid2 = CreatePerson(family: user.Person.Family, positionInFamilyId: PositionInFamily.Child, genderId: GenderCode.Female, age: 6);
                        
            Login();

            MaximizeWindow();

            Open($"{rootUrl}NewQuery", true);

            Wait(10);
            Find(css: "#QueryConditionSelect #Age").Click();

            Wait(10);
            Find(xpath: "//input[@id='IntegerValue']").SendKeys("6");
            Find(xpath: "//*[@id='SaveCondition']").Click();
            Wait(5);
            Find(id: "Run").Click();

            //Execution of query with only one condition            
            Wait(2);
            var resultCond1 = Find(xpath: "//div[@id='results']/div/div/h5").Text;
            

            //Add Parents only condition
            Find(xpath: "//a[contains(@class, 'addnewclause btn btn-xs btn-success')]").Click();
            Wait(2);
            Find(xpath: "//input[@class='form-control searchConditions']").SendKeys("Parent");
            Wait(2);
            Find(id: "ParentsOf").Click();
            Wait(2);
            Find(xpath: "//*[@id='SaveCondition']").Click();
            Wait(2);
            Find(id: "Run").Click();

            //Execution of query with two conditions            
            Wait(2);
            var resultCond2 = Find(xpath: "//div[@id='results']/div/div/h5").Text;
            
            //Disable latest condition
            Find(xpath: "//span[text()='Parents Only = True']/parent::*/parent::*/div/div/a[@class='dropdown-toggle hidden-xs']").Click();
            Wait(2);
            Find(xpath: "//span[text()='Parents Only = True']/parent::*/parent::*/div/div/ul/li/a[contains(@class, 'toggleConditionEnabled')]").Click();
            Wait(2);
            Find(id: "Run").Click();

            //Execution of query with two conditions but latest one is disabled
            //
            Wait(2);
            var resultCond3 = Find(xpath: "//div[@id='results']/div/div/h5").Text;

            //Enable latest condition
            Find(xpath: "//span[text()='Parents Only = True']/parent::*/parent::*/div/div/a[@class='dropdown-toggle hidden-xs']").Click();
            Wait(2);
            Find(xpath: "//span[text()='Parents Only = True']/parent::*/parent::*/div/div/ul/li/a[contains(@class, 'toggleConditionEnabled')]").Click();
            Wait(2);
            Find(id: "Run").Click();
            Wait(2);
            var resultCond4 = Find(xpath: "//div[@id='results']/div/div/h5").Text;

            resultCond1.Substring(0, resultCond1.IndexOf(' ')).ShouldBe(resultCond3.Substring(0, resultCond3.IndexOf(' ')));
            resultCond2.Substring(0, resultCond2.IndexOf(' ')).ShouldBe(resultCond4.Substring(0, resultCond4.IndexOf(' ')));
            resultCond1.Substring(0, resultCond1.IndexOf(' ')).ShouldNotBe(resultCond2.Substring(0, resultCond2.IndexOf(' ')));
            resultCond2.Substring(0, resultCond2.IndexOf(' ')).ShouldNotBe(resultCond3.Substring(0, resultCond3.IndexOf(' ')));
        }
    }
}
