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 SequenceStepDefinitions
    {
        private readonly IWebDriver driver;
        private readonly BasicPage basicPage;
        private readonly AddSequencePage addSequencePage;
        private readonly AutomationTestManagerHelper automationTestManagerHelper;
        private readonly ScenarioContext _scenarioContext;

        public SequenceStepDefinitions(ScenarioContext scenarioContext)
        {
            driver = WebDriverHelper.GetWebDriver();
            basicPage = new(driver);
            addSequencePage = new(driver);
            _scenarioContext = scenarioContext;
            automationTestManagerHelper = new(_scenarioContext);
        }

        [BeforeScenario("createsequence", Order = 14)]
        public async Task GivenCreateSequenceViaApi()
        {
          await automationTestManagerHelper.CreateSequence();
        }

        [Given(@"I create a new Sequence via API")]
        public async Task GivenCreateSequenceViaApi(Table dataTable)
        {
            var sequences = dataTable.CreateSet<SequenceData>();
            foreach (var sequence in sequences)
            {
               await automationTestManagerHelper.CreateSequence(sequence.Name);
            }
        }

        [Given(@"I create a new Sequence")]
        [When(@"I create a new Sequence")]
        public void GivenICreateASequenceWithRolloverTypeDay(Table dataTable)
        {
            BasicPage.ClickOnDropdownMenuItem("Sequence");

            var sequences = dataTable.CreateSet<SequenceData>();

            foreach (var sequence in sequences)
            {

                _scenarioContext["SequenceName"] = sequence.Name + _scenarioContext["GUIDPostfix"];

                I.Click(BasicPage.AddButton);
                I.FillField(BasicPage.NameField, _scenarioContext["SequenceName"].ToString()!);
                I.FillField(AddSequencePage.SeedField, sequence.Seed!);
                I.FillField(AddSequencePage.IncrementField, sequence.Increment!);
                I.FillField(AddSequencePage.PatternField, sequence.Pattern!);
                I.SelectOption(AddSequencePage.RolloverTypeSelector, sequence.RolloverType!);
                I.Click(BasicPage.SaveAndCloseButton);
                AutomationTestManagerHelper.SaveTimingForAction(_scenarioContext);
                BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["SequenceName"].ToString()!);
                _scenarioContext["SequenceCreated"] = $"{"false"}, {sequence.Increment}, {_scenarioContext["SequenceName"]}, {sequence.Pattern!}, " +
                    $"{sequence.RolloverType}, {sequence.Seed!}";
                I.WaitTillInvisible(BasicPage.SaveAndCloseButton);

            }
        }

        [Given(@"I update existing Sequence")]
        [When(@"I update existing Sequence")]
        public void WhenIUpdateASequence(Table dataTable)
        {
            BasicPage.ClickOnDropdownMenuItem("Sequence");
            BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["SequenceName"].ToString()!);
            var sequences = dataTable.CreateSet<SequenceData>();

            foreach (var sequence in sequences)
            {

                I.Click(BasicPage.EditObjectButton(_scenarioContext["SequenceName"].ToString()!));
                _scenarioContext["SequenceName"] = sequence.Name + _scenarioContext["GUIDPostfix"];
                I.FillField(BasicPage.NameField, _scenarioContext["SequenceName"].ToString()!);
                I.FillField(AddSequencePage.SeedField, sequence.Seed!);
                I.FillField(AddSequencePage.IncrementField, sequence.Increment!);
                I.FillField(AddSequencePage.PatternField, sequence.Pattern!);
                I.SelectOption(AddSequencePage.RolloverTypeSelector, sequence.RolloverType!);
                I.Click(BasicPage.SaveAndCloseButton);
                AutomationTestManagerHelper.SaveTimingForAction(_scenarioContext);
                _scenarioContext["SequenceEdited"] = $"{sequence.Increment}, {_scenarioContext["SequenceName"]}, {sequence.Pattern!}, " +
                    $"{sequence.RolloverType}, {sequence.Seed!}";
                I.WaitTillInvisible(BasicPage.SaveAndCloseButton);

            }
        }

        [When(@"I delete a sequence")]
        public void WhenIDeleteASequence()
        {
            BasicPage.ClickOnDropdownMenuItem("Sequence");
            BasicPage.SearchObjectNameInTableViaSearchInputField(_scenarioContext["SequenceName"].ToString()!);
            I.Click(BasicPage.DeleteObjectButton(_scenarioContext["SequenceName"].ToString()!));
            I.WaitTillInvisible(BasicPage.DeleteObjectButton(_scenarioContext["SequenceName"].ToString()!));
            I.Click(BasicPage.ConfirmDeletionButton);
            AutomationTestManagerHelper.SaveTimingForAction(_scenarioContext);
            BasicPage.SearchDeletedObjectNameInTableViaSearchInputField(_scenarioContext["SequenceName"].ToString()!);
        }
    }
}
