using OpenQA.Selenium.Appium.Windows;

namespace Tests.E2ETests {
    [TestFixture, Order(3)]
    public class MemberAdminWindowTest {

        private AppSession Session;
        private WindowsDriver<WindowsElement> Window;

        [OneTimeSetUp]
        public void WindowSetup() {
            Session = new(Utils.ApplicationPath, Utils.ConfigPath, WinAppDriver.WinAppDriverUrl);
            Session.App.FindElement(By.Name("Mitglieder")).Click();
            Thread.Sleep(Utils.WINDOW_OPEN_SLEEP);
            Window = Session.CreateWindowDriver("MemberAdminWindow");
        }

        [OneTimeTearDown]
        public void WindowTeardown() {
            Window.Close();
            Window.Quit();
            Session.Dispose();
        }

        [TearDown]
        public void Teardown() {
            Window.FindElement(By.WpfId("SearchInput")).Clear();
            Thread.Sleep(500);
        }

        [Test]
        public void Test_1_CreateMember() {
            Window.FindElement(By.WpfId("NewMemberButton")).Click();

            Window.FindElement(By.WpfId("MgNrInput")).Clear();
            Window.FindElement(By.WpfId("MgNrInput")).SendKeys("9999");

            Window.FindElement(By.WpfId("GivenNameInput")).SendKeys("Norbert");
            Window.FindElement(By.WpfId("NameInput")).SendKeys("Neuling");
            Window.FindElement(By.WpfId("PrefixInput")).SendKeys("Ing.");
            Window.FindElement(By.WpfId("SuffixInput")).SendKeys("jun.");
            Window.FindElement(By.WpfId("BirthdayInput")).SendKeys("1987");

            Window.FindElement(By.WpfId("AddressInput")).SendKeys("Musterstraße 9");
            Window.FindElement(By.WpfId("PlzInput")).SendKeys("2120");
            Window.FindElement(By.WpfId("OrtInput")).SelectItem(1);

            Window.FindElement(By.WpfId("EmailAddress1Input")).SendKeys("norbert.neuling@aon.at");
            Window.FindElement(By.WpfId("EmailAddress2Input")).SendKeys("nathalie.neuling@aon.at");

            Window.FindElement(By.WpfId("PhoneNr1TypeInput")).SelectItem(1);
            Window.FindElement(By.WpfId("PhoneNr1Input")).SendKeys("012345678");

            Window.FindElement(By.WpfId("PhoneNr2TypeInput")).SelectItem(2);
            Window.FindElement(By.WpfId("PhoneNr2Input")).SendKeys("0664123456");

            Window.FindElement(By.WpfId("IbanInput")).SendKeys("AT611904300234573201");
            Window.FindElement(By.WpfId("BicInput")).SendKeys("RLNWATWWWDF");

            Window.FindElement(By.WpfId("UstIdNrInput")).SendKeys("ATU66192906");  // TODO: Testdaten?
            Window.FindElement(By.WpfId("LfbisNrInput")).SendKeys("1251074");  // TODO: Testdaten?

            Window.FindElement(By.WpfId("BuchführendInput")).Click();
            Window.FindElement(By.WpfId("OrganicInput")).Click();

            Window.FindElement(By.WpfId("BillingNameInput")).SendKeys("Neuling KG");
            Window.FindElement(By.WpfId("BillingAddressInput")).SendKeys("Betriebsstraße 1");
            Window.FindElement(By.WpfId("BillingPlzInput")).SendKeys("2120");
            Window.FindElement(By.WpfId("BillingOrtInput")).SelectItem(2);

            Window.FindElement(By.WpfId("BusinessSharesInput")).SendKeys("10");
            Window.FindElement(By.WpfId("BranchInput")).SelectItem("Matzen");
            Window.FindElement(By.WpfId("DefaultKgInput")).SelectItem(3);

            Window.FindElement(By.WpfId("VollLieferantInput")).Click();
            Window.FindElement(By.WpfId("FunkionärInput")).Click();

            Window.FindElement(By.WpfId("CommentInput")).SendKeys("Die lieben Mustermänner und Musterfrauen!");
            Window.FindElement(By.WpfId("ContactEmailInput")).Click();

            Window.FindElement(By.WpfId("SaveButton")).Click();

            Window.FindElement(By.WpfId("SearchInput")).SendKeys("9999");
            Thread.Sleep(500);
            var memberListRow = Window.FindElement(By.WpfId("MemberList")).FindElement(By.ClassName("DataGridRow"));
            Assert.Multiple(() => {
                Assert.That(memberListRow, Is.Not.Null);
                Assert.That(memberListRow.FindElement(By.Name("9999 ")), Is.Not.Null);
                Assert.That(memberListRow.FindElement(By.Name("Norbert")), Is.Not.Null);
                Assert.That(memberListRow.FindElement(By.Name("Neuling")), Is.Not.Null);
            });
        }

        [Test]
        public void Test_2_EditMember() {
            Window!.FindElement(By.WpfId("SearchInput")).SendKeys("9999");
            Thread.Sleep(500);
            var memberList = Window.FindElement(By.WpfId("MemberList"));
            Assert.That(memberList, Is.Not.Null);

            var memberListRows = memberList.FindElements(By.ClassName("DataGridRow"));
            Assert.That(memberListRows, Has.Count.EqualTo(1));

            Window.FindElement(By.WpfId("EditMemberButton")).Click();

            var businessSharesInput = Window.FindElement(By.WpfId("BusinessSharesInput"));
            Assert.That(businessSharesInput, Is.Not.Null);

            var businessShares = int.Parse(businessSharesInput.Text);
            businessSharesInput.Clear();
            businessSharesInput.SendKeys($"{businessShares + 5}");

            Window.FindElement(By.WpfId("SaveButton")).Click();

            var newBusinessShares = int.Parse(businessSharesInput.Text);
            Assert.That(newBusinessShares, Is.EqualTo(businessShares + 5));
        }

        [Test]
        public void Test_3_DeleteMember() {
            Window!.FindElement(By.WpfId("SearchInput")).SendKeys("9999");
            Thread.Sleep(500);
            var memberList = Window.FindElement(By.WpfId("MemberList"));
            Assert.That(memberList, Is.Not.Null);

            var memberListRows = memberList.FindElements(By.ClassName("DataGridRow"));
            Assert.That(memberListRows, Has.Count.EqualTo(1));

            var memberListRow = memberListRows.First();
            Assert.Multiple(() => {
                Assert.That(memberListRow, Is.Not.Null);
                Assert.That(memberListRow.FindElement(By.Name("9999 ")), Is.Not.Null);
                Assert.That(memberListRow.FindElement(By.Name("Norbert")), Is.Not.Null);
                Assert.That(memberListRow.FindElement(By.Name("Neuling")), Is.Not.Null);
            });

            Window.FindElement(By.WpfId("DeleteMemberButton")).Click();
            var dialog = Session.CreateWindowDriver("DeleteMemberDialog");
            dialog.FindElement(By.WpfId("NameInput")).SendKeys("9999 Ing. Norbert Neuling jun.");
            dialog.FindElement(By.WpfId("ConfirmButton")).Click();

            memberListRows = memberList.FindElements(By.ClassName("DataGridRow"));
            Assert.That(memberListRows, Has.Count.EqualTo(0));
        }
    }
}