namespace Demo
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using Applitools;
    using Applitools.Selenium;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;

    public class LongPageTesting {
        public static void Main(string[] args) {

            var driver = new ChromeDriver();

            var eyes = new Eyes();

            eyes.SetLogHandler(new StdoutLogHandler(true));

            try
            {
                var eyesDriver = eyes.Open(driver, "Applitools", "Long page testing example - C#", new Size(1200, 600));

                eyesDriver.Url = "https://help.applitools.com/hc/en-us/articles/360041392472-Long-page-testing";

                // Save original configuration
                StitchModes originalStitchMode = eyes.StitchMode;
                var originalFullPageMode = eyes.ForceFullPageScreenshot;
                // Change configuration
                eyes.StitchMode = StitchModes.CSS;
                eyes.ForceFullPageScreenshot = true;

                // Get the size and location of the main element
                IWebElement element = eyesDriver.FindElement(By.CssSelector("html"));
                Size size = element.Size;
                Point location = element.Location;
                Rectangle elementRect = new Rectangle(location, size);

                // Set the size for each Region
                int maxLengthOfEachScreenshot = 10000;

                // Divide the element into "chunks" of size under 15k pixels,
                // and create a list of Targets
                Rectangle rect;
                List<ICheckSettings> targets = new List<ICheckSettings>();

                for (int i = location.Y; i < location.Y + size.Height; i += maxLengthOfEachScreenshot)
                {
                    if (elementRect.Bottom > i + maxLengthOfEachScreenshot)
                    {
                        rect = new Rectangle(location.X, i, size.Width, maxLengthOfEachScreenshot);
                    }
                    else
                    {
                        rect = new Rectangle(location.X, i, size.Width, elementRect.Bottom - i);
                    }
                    targets.Add(Target.Region(rect));
                }

                // Capture the list of Targets with a single scroll
                eyes.Check(targets.ToArray());

                eyes.StitchMode = originalStitchMode;
                eyes.ForceFullPageScreenshot = originalFullPageMode;

                // End the test.
                eyes.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                driver.Quit();

                eyes.AbortIfNotClosed();
            }
        }
    }
}