using System;
using System.IO;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using EasyTabs;
using Google.Apis.Safebrowsing.v4;
using Google.Apis.Safebrowsing.v4.Data;
using Google.Apis.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace XeroBrowser
{
    
    public static class Program
    {
        //safebrowsing integration
        /* public static async Task<bool> IsUrlUnsafe(string url, string apiKey)
        {
            // ignore any file stored locally
            if (url.StartsWith("file://"))
            {
                return false;
            }

            var safeBrowsingService = new SafebrowsingService(new BaseClientService.Initializer
            {
                ApiKey = "AIzaSyCQV - s52iNah - il6T5iFuqo6M_JzcLyaxs",
                ApplicationName = "XeroBrowser"
            });

            var request = new GoogleSecuritySafebrowsingV4FindThreatMatchesRequest
            {
                Client = new GoogleSecuritySafebrowsingV4ClientInfo { ClientId = "XeroBrowser", ClientVersion = "1.0" },
                ThreatInfo = new GoogleSecuritySafebrowsingV4ThreatInfo
                {
                    ThreatTypes = new List<string>
               {
                   "MALWARE",
                   "SOCIAL_ENGINEERING",
                   "UNWANTED_SOFTWARE",
                   "POTENTIALLY_HARMFUL_APPLICATION"
               },
                    PlatformTypes = new List<string> { "ANY_PLATFORM" },
                    ThreatEntryTypes = new List<string> { "URL" },
                    ThreatEntries = new List<GoogleSecuritySafebrowsingV4ThreatEntry> { new GoogleSecuritySafebrowsingV4ThreatEntry { Url = url } }
                }
            };

            var response = await safeBrowsingService.ThreatMatches.Find(request).ExecuteAsync();
            try { }
                catch(System.Net.Http.HttpRequestException) { }
            
            return response != null && response.Matches != null && response.Matches.Count > 0;
        }
        */
        public static AppContainer Container;

        private static TitleBarTabsApplicationContext _applicationContext;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Container = new AppContainer();
            CefSettings settings = new CefSettings();
            settings.CefCommandLineArgs["plugin-policy"] = "allow";
            settings.UserAgent = "Mozilla/5.0 (Windows NT 11.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 XeroBrowser/1.0.0";
            settings.CefCommandLineArgs.Add("enable-media-stream", "1");
            settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\CEF";
            settings.CefCommandLineArgs.Add("safebrowsing-disable-download-protection", "0");
            settings.CefCommandLineArgs.Add("safebrowsing-disable-extension-blacklist", "0");
            /// settings.CefCommandLineArgs.Add("safebrowsing-disable-auto-update", "0");
            FileChecker.FolderCheck();
            FileChecker.FileCheck();

            Cef.Initialize(settings);

            Container.Tabs.Add(

                new TitleBarTab(Container)
                {
                    Content = new FrmBrowser
                    {
                        Text = @"New Tab"
                    }
                }

            );


            Container.SelectedTabIndex = 0;

            try { 
                _applicationContext = new TitleBarTabsApplicationContext();
                _applicationContext.Start(Container);
                Application.Run(_applicationContext);
            }
            catch (Exception)
            {
                // ignored
            }
        }
    }
}