using ESuite.UI.E2E.Helpers;
using ESuite.UI.E2E.Models;
using ESuite.UI.E2E.Pages;
using OpenQA.Selenium;

namespace ESuite.UI.E2E.StepDefinitions
{
    [Binding]
    public class OrganisationCreationStepDefinitions
    {
        private readonly ScenarioContext _scenarioContext;
        private readonly AutomationTestManagerHelper automationTestManagerHelper;
        public OrganisationCreationStepDefinitions(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
            automationTestManagerHelper = new(_scenarioContext);
        }

        [BeforeScenario("createorganisation", Order = 10)]
        public async Task CreateOrganisation()
        {
            await automationTestManagerHelper.CreateOrganisation();
        }

        [BeforeScenario("createorganisation_site", Order = 11)]
        public void CreateOrganisationAndLinkedSite()
        {
            //this request triggers create organisation request
            CreateOrganisation().GetAwaiter().GetResult();

            automationTestManagerHelper.CreateSite();
        }

        [BeforeScenario("createsite", Order = 12)]
        public void CreateSite()
        {
            automationTestManagerHelper.CreateSite();
        }

        [AfterScenario("deleteorganisation", Order = 99)]
        [When(@"I delete existing Organisation")]
        public void DeleteOrganisation()
        {
            automationTestManagerHelper.DeleteOrganisation();
        }

        [Given(@"I create a new organisation")]
        public void GivenICreateANewOrganisation(Table dataTable)
        {
            I.Click(BasicPage.DropdownTab("e-print"));

            var organisations = dataTable.CreateSet<OrganisationsData>();

            foreach (var organisation in organisations)
            {
                _scenarioContext["OrganisationName"] = organisation.Name + _scenarioContext["GUIDPostfix"];

                I.Click(BasicPage.AddButton);
                I.FillField(BasicPage.NameField, _scenarioContext["OrganisationName"].ToString()!);
                I.FillField(BasicPage.AddressField, organisation.Address!);
                I.SelectOption(BasicPage.SelectorByLabel("Status"), organisation.Status!);
                I.Click(BasicPage.SaveAndCloseButton);
                I.WaitTillInvisible(BasicPage.SaveAndCloseButton);
                BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["OrganisationName"].ToString()!);
            }
        }

        [Then(@"I add a new site")]
        public void ThenIAddANewSite(Table dataTable)
        {
            I.Click(BasicPage.DropdownTab("e-print"));

            var sites = dataTable.CreateSet<SiteData>();

            foreach (var site in sites)
            {
                _scenarioContext["SiteName"] = site.Name + _scenarioContext["GUIDPostfix"];

                I.Click(By.LinkText(_scenarioContext["OrganisationName"].ToString()!));

                I.Click(BasicPage.AddButton);
                I.FillField(BasicPage.NameField, _scenarioContext["SiteName"].ToString()!);
                I.FillField(BasicPage.AddressField, site.Address!);
                I.SelectOption(BasicPage.SelectorByLabel("Status"), site.Status!);
                I.Click(BasicPage.SaveAndCloseButton);
                I.WaitTillInvisible(BasicPage.SaveAndCloseButton);
                AutomationTestManagerHelper.SaveTimingForAction(_scenarioContext);
                BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["SiteName"].ToString()!);
            }
        }

        [When(@"I edit existing Organisation")]
        public void WhenIEditAnExistingOrganisation(Table dataTable)
        {
            I.Click(BasicPage.DropdownTab("e-print"));

            var organisations = dataTable.CreateSet<OrganisationsData>();

            foreach (var organisation in organisations)
            {
                I.Click(BasicPage.EditOrganisationButton(_scenarioContext!["OrganisationName"].ToString()!));

                _scenarioContext!["OrganisationName"] = organisation.Name + _scenarioContext["GUID"];
                I.FillField(BasicPage.NameField, _scenarioContext["OrganisationName"].ToString()!);
                I.FillField(BasicPage.AddressField, organisation.Address!);
                I.SelectOption(BasicPage.SelectorByLabel("Status"), organisation.Status!);
                I.Click(BasicPage.SaveAndCloseButton);
                AutomationTestManagerHelper.SaveTimingForAction(_scenarioContext);
                I.WaitTillInvisible(BasicPage.SaveAndCloseButton);
                BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["OrganisationName"].ToString()!);
                _scenarioContext["OrganisationEdited"] = $"{organisation.Address!}, {_scenarioContext!["OrganisationName"]}, {organisation.Status!}";
            }
        }

        [When(@"I edit existing Site")]
        [Given(@"I edit existing Site")]
        public void GivenIEditAnExistingUser(Table dataTable)
        {
            I.Click(BasicPage.DropdownTab("Home"));
            I.Click(BasicPage.DropdownTab("e-print"));
            I.Click(BasicPage.EPrintInstanceButton(_scenarioContext!["OrganisationName"].ToString()!));

            var sites = dataTable.CreateSet<SiteData>();

            foreach (var site in sites)
            {
                I.Click(BasicPage.EditOrganisationButton(_scenarioContext!["SiteName"].ToString()!));

                _scenarioContext["SiteName"] = site.Name + _scenarioContext["GUIDPostfix"];

                I.FillField(BasicPage.NameField, _scenarioContext["SiteName"].ToString()!);
                I.FillField(BasicPage.AddressField, site.Address!);
                I.SelectOption(BasicPage.SelectorByLabel("Status"), site.Status!);
                I.Click(BasicPage.SaveAndCloseButton);
                AutomationTestManagerHelper.SaveTimingForAction(_scenarioContext);
                I.WaitTillInvisible(BasicPage.SaveAndCloseButton);
                BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["SiteName"].ToString()!);
                _scenarioContext["SiteEdited"] = $"{site.Address!}, {_scenarioContext!["SiteName"]}, {site.Status!}";

            }
        }
    }
}
