using FtxDemo.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FtxDemo {
    class Program {
        static void Main(string[] args) {
            using (var dc = new FtxDemoContext()) {
                dc.Database.Log = Console.WriteLine;

                // Using LIKE
                Console.WriteLine("Found using LIKE '%kůň%':");
                var s = "kůň";
                var q = dc.Stories.Where(x => x.Body.Contains(s)).ToList();
                PrintStoryList(q);

                // Using FTX
                Console.WriteLine("Found using FTX - kůň inflectional:");
                s = FtxInterceptor.CreateQuery("kůň");
                q = dc.Stories.Where(x => x.Body.Contains(s)).ToList();
                PrintStoryList(q);
                Console.WriteLine();

                // Using complex FTX
                Console.WriteLine("Found using FTX - pán not soudce:");
                s = FtxInterceptor.CreateQuery("pán NOT soudce");
                q = dc.Stories.Where(x => x.Body.Contains(s)).ToList();
                PrintStoryList(q);
                Console.WriteLine();

                Console.WriteLine("The same query ordered by rank descending:");
                q = dc.Stories.FulltextSearch("pán").ToList();
                PrintStoryList(q);
                Console.WriteLine();

                Console.WriteLine("Using queryable TVF:");
                q = dc.StoriesFulltext("pán").Where(x => x.Title.Contains("z")).ToList();
                PrintStoryList(q);
                Console.WriteLine();
            }
        }

        static void PrintStoryList(IEnumerable<Story> list) {
            if (!list.Any()) {
                Console.WriteLine("No stories found.");
            }
            else {
                Console.WriteLine("  ID  Title");
                Console.WriteLine("----  --------------------------------------------------");
                int i = 0;
                foreach (var story in list) {
                    Console.WriteLine($"{story.Id,4}  {story.Title}");
                    i++;
                }
                Console.WriteLine($"({i} stories found)");
            }
            Console.WriteLine();
        }

    }
}
