using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
using Tests.UnitTests.WeighingTests;

namespace Tests.E2ETests {
    [TestFixture, Order(2)]
    public class DeliveryAdminWindowReceiptTest {

        private MockScale Mock;
        private AppSession Session;

        [OneTimeSetUp]
        public void Setup() {
            Mock = new CommandMockScale(12345, ScaleHandlers.Handle_IT3000A) {
                Weight = 3210,
            };
            Session = new(Utils.ApplicationPath, Utils.ConfigPath, WinAppDriver.WinAppDriverUrl);
            Session.App.FindElement(By.Name("Stammdaten")).Click();
            Thread.Sleep(500);
            var window = Session.CreateWindowDriver("BaseDataWindow");
            window.FindElement(By.Name("Saisons")).Click();
            window.FindElement(By.Name("Neu anlegen...")).Click();
            var dialog = Session.CreateWindowDriver("NewSeasonDialog");
            dialog.FindElement(By.Name("Bestätigen")).Click();
            dialog.Close();
            Thread.Sleep(500);
            window.Close();
        }

        [OneTimeTearDown]
        public void Teardown() {
            Session.Dispose();
            Mock.Dispose();
        }

        private WindowsDriver<WindowsElement> OpenReceiptWindow() {
            Session.App.FindElement(By.Name("Übernahme")).Click();
            Thread.Sleep(Utils.WINDOW_OPEN_SLEEP);
            return Session.CreateWindowDriver("DeliveryAdminWindow");
        }

        private void FinishDeliveryNote(WindowsDriver<WindowsElement> window) {
            var gerebeltGewogen = window.FindElement(By.WpfId("GerebeltGewogenInput"));
            gerebeltGewogen.Click();
            gerebeltGewogen.Click();
            Thread.Sleep(500);
            window.FindElement(By.Name("Abschließen")).Click();
            Thread.Sleep(2000);
            var doc = Session.CreateWindowDriver("DocumentViewerWindow");
            Assert.That(doc.Title, Contains.Substring("Traubenübernahmeschein"));
            Thread.Sleep(500);
            doc.Close();
            Thread.Sleep(500);
        }

        [Test]
        public void Test_1_Minimal() {
            var window = OpenReceiptWindow();
            window.FindElement(By.WpfId("MgNrInput")).SendKeys("101" + Keys.Enter + "GV" + Keys.Enter + "73" + Keys.Enter + Keys.Enter);
            FinishDeliveryNote(window);
            window.Close();
        }

        [Test]
        public void Test_2_OtherInputs() {
            var window = OpenReceiptWindow();
            window.FindElement(By.WpfId("MemberInput")).SendKeys("Mustermann Max");
            window.FindElement(By.WpfId("WineVarietyInput")).SelectItem("Zweigelt");
            window.FindElement(By.WpfId("GradationKmwInput")).SendKeys("18");
            window.FindElement(By.Name("Wiegen")).Click();
            FinishDeliveryNote(window);
            window.Close();
        }

        [Test]
        public void Test_3_AttributeCultivationModifier() {
            var window = OpenReceiptWindow();
            window.FindElement(By.WpfId("MgNrInput")).SendKeys("102" + Keys.Enter + "GVK");
            window.FindElement(By.WpfId("CultivationInput")).SelectItem("Bio");
            window.FindElement(By.WpfId("GradationOeInput")).SendKeys("73" + Keys.Enter + Keys.Enter);
            FinishDeliveryNote(window);
            window.Close();
        }
    }
}
