using System.IO;
using System.Linq;
using System.Runtime;

namespace ZionExtractor.Controllers.LegworkMethods
{
    class BetweenText
    {
        public static ZionExtractor.Models.Hypothesis Search(ZionVault.Binder.OCR.Page page, ZionVault.Basic.Location limitA, ZionVault.Basic.Location limitB, ZionSchematics.Models.SearchOptions options)
        {
            double limitAOffset = ((limitA.Bottom - limitA.Top) / 2);
            double limitBOffset = ((limitB.Bottom - limitB.Top) / 2);

            try
            {
                var ListOfOptions = (from areas in page.Areas
                                     from paragraphes in areas.Paragraphes
                                     from lines in paragraphes.Lines
                                     from word in lines.Words
                                     where
                                         word != null
                                         && (
                                                (
                                                    (limitA.Top == limitB.Top && limitA.Bottom == limitB.Bottom)
                                                    && (word.Location.Left >= limitA.Right && word.Location.Right <= limitB.Left)
                                                    && (word.Location.Top >= limitA.Top && word.Location.Bottom <= limitA.Bottom)
                                                )
                                                ||
                                                (
                                                    (limitA.Bottom <= limitB.Top || limitB.Top == -1)
                                                    && (word.Location.Left > limitA.Right)
                                                    && (word.Location.Top >= (limitA.Top - limitAOffset) && word.Location.Bottom <= (limitA.Bottom + limitAOffset))
                                                )
                                                ||
                                                (
                                                    (limitA.Bottom <= limitB.Top || limitB.Top == -1)
                                                    && (word.Location.Right <= limitB.Left)
                                                    && (word.Location.Top >= (limitB.Top - limitBOffset) && word.Location.Bottom <= (limitB.Bottom + limitBOffset))
                                                )
                                            )
                                         && (options.MinLength == 0 || word.Text.Length >= options.MinLength)
                                         && (options.MaxLength == 0 || word.Text.Length <= options.MaxLength)
                                     select word).ToList();

                ZionExtractor.Models.Hypothesis hypothesis = new ZionExtractor.Models.Hypothesis(
                        string.Empty,
                        new ZionVault.Basic.Location(@"0;0;0;0"),
                        (int)ListOfOptions.Average(r => r.Quality));

                foreach (ZionVault.Binder.OCR.Word option in ListOfOptions)
                    hypothesis.Value += " " + option.Text;

                hypothesis.Value = hypothesis.Value.TrimStart();

                return hypothesis;
            }
            catch
            {
                return null;
            }
        }
    }


    class BetweenTextVertical
    {
        public static ZionExtractor.Models.Hypothesis Search(ZionVault.Binder.OCR.Page page, ZionVault.Basic.Location limitA, ZionVault.Basic.Location limitB, ZionSchematics.Models.SearchOptions options)
        {
            double limitAOffset = ((limitA.Bottom - limitA.Top) / 2);
            double limitBOffset = ((limitB.Bottom - limitB.Top) / 2);

            try
            {
                var ListOfOptions = (from areas in page.Areas
                                     from paragraphes in areas.Paragraphes
                                     from lines in paragraphes.Lines
                                     from word in lines.Words
                                     where
                                         word != null
                                         && (
                                                    word.Location.Top > limitA.Top+40 &&
                                                    word.Location.Bottom < limitB.Bottom-40 &&
                                                    word.Location.Left > options.Rect.Left &&
                                                    word.Location.Right < options.Rect.Right
                                            )
                                         && (options.MinLength == 0 || word.Text.Length >= options.MinLength)
                                         && (options.MaxLength == 0 || word.Text.Length <= options.MaxLength)
                                     select word).ToList();

                ZionExtractor.Models.Hypothesis hypothesis = new ZionExtractor.Models.Hypothesis(
                         string.Empty,
                         new ZionVault.Basic.Location(@"0;0;0;0"),
                         (int)ListOfOptions.Average(r => r.Quality));

                foreach (ZionVault.Binder.OCR.Word option in ListOfOptions)
                    hypothesis.Value += " " + option.Text;

                hypothesis.Value = hypothesis.Value.TrimStart();

                return hypothesis;
            }
            catch
            {
                return null;
            }
        }
    }

}