using System;
using System.Collections.Generic;
using System.Linq;

using FairyGUI;

using UnityEngine;
using UnityEngine.EventSystems;
using BingoBrain;
using DG.Tweening;
using BingoBrain;
using BingoBrain.Core;
using BingoBrain.HotFix;
namespace DontConfuse
{


    public class WebviewManager : MonoBehaviour
    {
        //         Vector2 startPos;
        //         float lastTouchTime = 0f;
        //         float touchInterval = 0.3f;
        //         void Update()
        //         {
        // #if UNITY_IOS && !UNITY_EDITOR
        //             if(Input.touchCount > 0)
        //             {
        //                 Touch touch_ = Input.GetTouch(0);
        //
        //                 if(touch_.phase == TouchPhase.Began)
        //                 {
        //                     startPos = touch_.position;
        //                     lastTouchTime = Time.time;
        //                 }
        //                 if(touch_.phase == TouchPhase.Ended)
        //                 {
        //                     if ((Time.time - lastTouchTime) < touchInterval){
        //                         // if (WebViewMgr.Instance.WebUIType == WebUIType.H5 && EventSystem.current.currentSelectedGameObject == null){
        //                         //     BrigdeIOS.SetTouchWebview(true, true);
        //                         // }else{
        //                         //     BrigdeIOS.SetTouchWebview(true);
        //                         // }
        //                         BrigdeIOS.SetClickView();
        //                     }
        //                 }
        //                 // if(touch_.phase == TouchPhase.Moved){
        //                 //     Vector2 delta = touch_.position - startPos;
        //                 // // Debug.Log("delta Y: " + delta.y);
        //                 //     BrigdeIOS.ScrollWebview(delta.x, delta.y);
        //                 //     startPos = touch_.position;
        //                 // }
        //             }
        // #endif
        //         }
        public static WebviewManager Instance;
        public WebviewManager()
        {
            Instance = this;
        }
        public void OpenWebView(string url)
        {
            //Debug.Log("[WebviewManager] OpenWebView");
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.OpenWebview(url);
// #endif
        }
        public void showDarkWebview()
        {
            //Debug.Log("[WebviewManager] OpenWebView");
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.showDarkWebview();
// #endif
        }

        public void CloseWebview()
        {
            //Debug.Log("[WebviewManager] CloseWebview");
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.CloseWebview();
// #endif
        }

        public void SetOffset(int offset_y, int offset_y1)
        {
            // Debug.Log($"barry [WebviewManager] SetOffset:{offset_y}, {offset_y1}");
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.SetOffset(offset_y, offset_y1);
// #endif
        }

        public void SetPadding(float left, float top, float right, float bottom)
        {
            //    Debug.Log("[WebviewManager] SetPadding");    
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.SetPadding(left, top, right, bottom);
// #endif
        }
        public void SetDarkThough(bool though)
        {
            if (though)
            {
                if (UI.Instance.IsExistUI(UIConst.LevelSuccessUI) || UI.Instance.IsExistUI(UIConst.MakeupConfirmUI) ||
                 UI.Instance.IsExistUI(UIConst.NewTaskUI) || UI.Instance.IsExistUI(UIConst.PackrewardUI) || UI.Instance.IsExistUI(UIConst.SaveingPotUI)
                )
                {
                    return;
                }

            }
            //    Debug.Log("[WebviewManager] SetPadding");    
// #if UNITY_IOS && !UNITY_EDITOR
//         BrigdeIOS.SetDarkThough(though);
// #endif
        }
        public void SetBtn(int left, int top, int right, int bottom)
        {
            //    Debug.Log("[WebviewManager] SetBtn");    
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.SetBtn(left, top, right, bottom);
// #endif
        }
        public void SetCTEnable(bool flag)
        {
            // Debug.Log("[WebviewManager] SetCTEnable"); 
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.SetCTEnable(flag);
// #endif
        }

        public void SetFullScreen()
        {
            // Debug.Log("[WebviewManager] SetFullScreen"); 
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.SetFullScreen();
// #endif
        }

        public void addH5Field(int field1, int field2, int field3, int field4, int field5, string field6, string field7, string dark_url, string light_url, bool is_gift, string web_through_str, string click_add_time)
        {
// #if UNITY_IOS && !UNITY_EDITOR
//            BrigdeIOS.addH5Field(field1,field2,field3,field4,field5,field6,field7,dark_url,light_url,is_gift,web_through_str,click_add_time);
// #endif
        }
        public void ShowH5View(bool flag)
        {
            // Debug.Log("[WebviewManager] SetCTEnable"); 
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.ShowH5View(flag);
// #endif
        }

        public void SetIconProgress(float val)
        {
//             // Debug.Log("[WebviewManager] SetCTEnable"); 
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.SetIconProgress(val);
// #endif
        }
        public void setInH5View(bool flag)
        {
            // Debug.Log("[WebviewManager] SetCTEnable"); 
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.setInH5View(flag);
// #endif
        }
        public void upDataH5times(string weblink, int times, bool is_dark)
        {
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.upDataH5times( weblink, times,is_dark);
// #endif
        }
        public void ShowFlyBtn(bool flag)
        {
            // Debug.Log("[WebviewManager] SetCTEnable"); 
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.ShowFlyBtn(flag);
// #endif
        }

        public void setFlyBtnTag(bool flag)
        {
            // Debug.Log($"[WebviewManager] setFlyBtnTag ---{flag}");
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.setFlyBtnTag(flag);
// #endif
        }

        public void setRewardBtnTag(bool flag)
        {
            // Debug.Log($"[WebviewManager] setRewardBtnTag--- {flag}");
// #if UNITY_IOS && !UNITY_EDITOR
//             BrigdeIOS.setRewardBtnTag(flag);
// #endif
        }

        public void ObjC_TouchClick(string name)
        {
            // Debug.Log("Touch click: " + name);
            GameDispatcher.Instance.Dispatch(BingoInfo.H5ViewClickBtn, name);
        }
        public void H5AutoRefresh(string times)
        {

        }


        private GList select_glist;
        private float select_glist_y;
               public Camera orthoCamera;

        public static Dictionary<string, string> adCallbackInfo = new Dictionary<string, string>();
        public void SendH5Event(string numbers)
        {

            // adCallbackInfo.Clear();
            // adCallbackInfo.Add("h5_revenue",ConfigSystem.GetConfig<CommonModel>().h5_refreshRevenue.ToString());
            // //Debug.Log("sssssssssssssssssss"+JsonConvert.SerializeObject(adCallbackInfo));
            // AppsFlyer.sendEvent("Growing_Total_01_002",adCallbackInfo);
        }

        public void RefreshUrl()
        {

            // if (gameUrlInfo == null) return;
            //TODO: show invisible h5 
            // var Dailyrefresh_reamain = 0;
            // var last_time = PlayerPrefs.GetInt("Dayreftimes", 0);
            // DateTime newDate = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            // newDate = newDate.AddSeconds(GameHelper.GetNowTime(true));
            // var newDays = newDate.Day;



            // if (last_time == newDays)
            // {
            //     Dailyrefresh_reamain = ConfigSystem.GetConfig<CommonModel>().Dailyrefreshtimes -
            //                            PlayerPrefs.GetInt("Dailyrefreshnum", 0);
            // }
            // else
            // {
            //     PlayerPrefs.SetInt("Dailyrefreshnum", 0);
            //     Dailyrefresh_reamain = ConfigSystem.GetConfig<CommonModel>().Dailyrefreshtimes;
            //     PlayerPrefs.SetInt("Dayreftimes", newDays);
            // }

            // int dark_last_time = PlayerPrefs.GetInt("dark_refreshDay", 0);

            // DateTime dark_newDate = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            // dark_newDate = dark_newDate.AddSeconds(GameHelper.GetNowTime(true));
            // var dark_newDays = dark_newDate.Day;


            // string darkWVDailyrefreshtimes_str = "";


            // if (SaveData.GetSaveobject().dark_Dayref == null)
            // {
            //     SaveData.GetSaveobject().dark_Dayref = new int[2];
            // }

            // if (dark_last_time == dark_newDays)
            // {
            //     // dark_Dailyrefresh_reamain = ConfigSystem.GetConfig<CommonModel>().darkWVDailyrefreshtimes[Int32.Parse(webview_index) - 1] -
            //     //                             PlayerPrefs.GetInt("dark_Dayref", 0);
            //     for (int i = 0; i < ConfigSystem.GetConfig<CommonModel>().darkWVDailyrefreshtimes.Length; i++)
            //     {

            //         if (i != 0) darkWVDailyrefreshtimes_str += "|";
            //         darkWVDailyrefreshtimes_str += ConfigSystem.GetConfig<CommonModel>().darkWVDailyrefreshtimes[i] - SaveData.GetSaveobject().dark_Dayref[i];

            //     }
            // }
            // else
            // {
            //     //  PlayerPrefs.SetInt("dark_Dayref", 0);
            //     for (int i = 0; i < SaveData.GetSaveobject().dark_Dayref.Length; i++)
            //     {
            //         SaveData.GetSaveobject().dark_Dayref[i] = 0;
            //     }
            //     SaveData.saveDataFunc();
            //     // dark_Dailyrefresh_reamain = ConfigSystem.GetConfig<CommonModel>().darkWVDailyrefreshtimes[Int32.Parse(webview_index)];

            //     for (int i = 0; i < ConfigSystem.GetConfig<CommonModel>().darkWVDailyrefreshtimes.Length; i++)
            //     {

            //         if (i != 0) darkWVDailyrefreshtimes_str += "|";
            //         darkWVDailyrefreshtimes_str += ConfigSystem.GetConfig<CommonModel>().darkWVDailyrefreshtimes[i];

            //     }

            //     PlayerPrefs.SetInt("dark_refreshDay", dark_newDays);
            // }

            // // var last_login_time = PlayerPrefs.GetInt("last_login_time", 0);//获取上次登录日期
            // // PlayerPrefs.SetInt("last_login_time", newDays);

            // string light_str = "";
            // string dark_str = "";

            // for (int i = 0; i < ConfigSystem.light_weblist.Count; i++)
            // {
            //     int can_refresh_numbners = 0;
            //     // if (last_login_time==newDays)
            //     //  {
            //     //     can_refresh_numbners=ConfigSystem.light_weblist[i].refreshMax- PlayerPrefs.GetInt(ConfigSystem.light_weblist[i].webLink, 0);
            //     //     if (can_refresh_numbners < 0) can_refresh_numbners = 0;
            //     // }
            //     // else
            //     //  {
            //     can_refresh_numbners = ConfigSystem.light_weblist[i].refreshMax;
            //     //if (can_refresh_numbners < 0) can_refresh_numbners = 9999999;
            //     // PlayerPrefs.SetInt(ConfigSystem.light_weblist[i].webLink, 0);
            //     // }

            //     if (i != 0)
            //     {
            //         light_str += "|";
            //     }

            //     light_str += ConfigSystem.light_weblist[i].webLink + "#" + ConfigSystem.light_weblist[i].probability + "#" + can_refresh_numbners + "#" + ConfigSystem.light_weblist[i].darkWebTimesCT; ;
            // }
            // for (int i = 0; i < ConfigSystem.dark_weblist.Count; i++)
            // {
            //     int can_refresh_numbners = 0;
            //     //  if (last_login_time==newDays)
            //     // {
            //     //    can_refresh_numbners=ConfigSystem.dark_weblist[i].refreshMax- PlayerPrefs.GetInt(ConfigSystem.dark_weblist[i].webLink, 0);
            //     //    if (can_refresh_numbners < 0) can_refresh_numbners = 0;
            //     // }
            //     // else
            //     // {
            //     can_refresh_numbners = ConfigSystem.dark_weblist[i].refreshMax;
            //     //if (can_refresh_numbners < 0) can_refresh_numbners = 9999999;
            //     //  PlayerPrefs.SetInt(ConfigSystem.dark_weblist[i].webLink, 0);
            //     //   }

            //     if (i != 0)
            //     {
            //         dark_str += "|";
            //     }

            //     dark_str += ConfigSystem.dark_weblist[i].webLink + "#" + ConfigSystem.dark_weblist[i].probability + "#" + can_refresh_numbners + "#" + ConfigSystem.dark_weblist[i].darkWebTimesCT + "#" + ConfigSystem.dark_weblist[i].wvType;
            // }


            // string darkWVRefreshtime_str = "";


            // for (int i = 0; i < ConfigSystem.GetConfig<CommonModel>().darkWVRefreshtime.Length; i++)
            // {
            //     if (i != 0) darkWVRefreshtime_str += "|";
            //     darkWVRefreshtime_str += ConfigSystem.GetConfig<CommonModel>().darkWVRefreshtime[i].ToString();
            // }
            // for (int i = 0; i < ConfigSystem.GetConfig<CommonModel>().darkWVRefreshtime2.Length; i++)
            // {
            //     darkWVRefreshtime_str += "|";
            //     darkWVRefreshtime_str += ConfigSystem.GetConfig<CommonModel>().darkWVRefreshtime2[i].ToString();
            // }

            // Debug.Log("--------------------------------");

            // string add_time = ConfigSystem.GetConfig<CommonModel>().WVClickCTAddTime1[0] + "|" +
            //  ConfigSystem.GetConfig<CommonModel>().WVClickCTAddTime1[1] + "|" + ConfigSystem.GetConfig<CommonModel>().WVClickCTAddTime2[0] + "|" + ConfigSystem.GetConfig<CommonModel>().WVClickCTAddTime2[1];

            // Debug.Log(add_time);
            // WebviewManager.Instance.addH5Field(ConfigSystem.GetConfig<CommonModel>().flyCtRate, ConfigSystem.GetConfig<CommonModel>().otherH5switch,
            //     ConfigSystem.GetConfig<CommonModel>().H5Refreshtime, Dailyrefresh_reamain, ConfigSystem.GetConfig<CommonModel>().darkThoughProbability
            //     , darkWVRefreshtime_str, darkWVDailyrefreshtimes_str, dark_str, light_str, GameHelper.IsGiftSwitch(), ConfigSystem.web_through_str, add_time);
            // WebviewManager.Instance.OpenWebView(gameUrlInfo.webLink);

            //WebviewManager.Instance.setFlyCtRate(ConfigSystem.GetConfig<CommonModel>().flyCtRate);
            // RefreshDataCT(isTop, gameUrlInfo);
        }
        public void ClickAdEvent(string ad_msg)
        {
            string[] temp_array = ad_msg.Split("|");
            if (temp_array.Length >= 2 && temp_array[0] != null && temp_array[0] != "")
            {
                H5sendClass info = new H5sendClass() { link = temp_array[0], type = temp_array[1] };
                NetworkKit.PostWithHeader<object>("/event/h5Clicks", info, (isSuccess, obj) =>
                {
                    // if (isSuccess)
                    // {
                    //     Debug.Log("dadianchenggong" + temp_array[0]);
                    // }
                });

            }

        }
        public static bool haveSimCard = false;
        public void diaoyongtest(string have)
        {
            if (have == "TRUE") haveSimCard = true;
        }

    }

    public class H5refreshTimes
    {
        public string link;
        public int times;

    }
    public class H5sendClass
    {
        public string link;
        public string type;

    }
}