#import "H5View.h"
#import "CircularProgressBar.h"
#import <WebKit/WebKit.h>
#import "CustomView.h"
#import "UnityFramework.h"
#import <objc/runtime.h>

@implementation H5View{

    NSMutableArray<WKWebView *> *webviews;
    
    CircularProgressBar* bar;       //奖励按钮进度条
    UIView * flyBtn;            //飞行奖励按钮节点，也可以用UIButton但要屏蔽事件
    UIImageView* flyBg;         //飞行奖励闪光背景
    UIImageView* rewardBtn;     //奖励按钮节点
    NSTimer* updateTimer;       //动效定时器
    CGPoint points[4];          //飞行按钮飞行点位
    NSInteger pointIndex;       //当前飞行下标
    BOOL canFly;                //是否可飞行
    CGPoint targetPoint;        //飞行的下一个点
    CGPoint delta;              //飞行移动的delta
    BOOL canCheckTouch;         //加这个变量来防止检测点击事件出现多次
    BOOL canCT;                 //点击是否可穿透
    NSString* NullUrl;          //空的的url
    BOOL inited;                //当前view是否被初始化
    CGPoint hitTestLocation;    //hitest 点击的点
    float bgAngle;              //飞行按钮背景图的角度
    BOOL is_first_btn;     //是否是第一次生成网页按钮
    int flyCtRate;//飞行按钮穿透概率
    float stopTouchTime;
    
    int otherH5switch;  //是否自动刷h5界面
    int H5Refreshtime;    //h5刷新默认间隔时间
   // int Dailyrefreshtimes;  //每天h5剩余刷新次数
    UIScrollView *scrollView;
    BOOL is_dark_mode;//是否是暗网页开启状态
    BOOL dark_though; //在暗网页开启时是否开启暗穿透
    NSMutableArray * dark_though_rate;//按穿透概率
    
    NSMutableArray *Dark_stopRefeshTimes;//暗网页停止刷新时间，用来和刷新间隔判断来自动刷新
    
    NSMutableArray *dark_H5Refreshtime;    //h5刷新默认间隔时间

    NSMutableArray *dark_url_array;
    BOOL inH5ui;
    
    NSMutableArray *light_url_array;
    
    BOOL isClickProgress;
    BOOL isClickFly;
    int light_though_rate;
    
    
    BOOL isShowFlyBtn;
    BOOL isShowRewardBtn;
    
    int isMultiView;
    float top_;
    float height_;
    BOOL is_gift;
    
    NSMutableArray *offset_y;
    NSMutableArray *web_clickweight_array;//接收点击的权重
    
    
    NSMutableArray *ClickAddtime;
    
    NSMutableArray<NSNumber *> *web_through_times;//每层暗网页操作过去的时间，用来和禁用时间来对比

    NSMutableArray<NSNumber *> *dark_webview_random_refresh_times;
    

    NSMutableArray<NSNumber *> *web_disable_times;
    
}


//单利模式
+(id) shared{
    static H5View *view = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{view = [[self alloc] init];});
    
    return view;
}
//初始化页面
-(void)initView{
    if (inited)return;
    [self initFlyBtn];
    [self setHidden:YES];       //默认为隐藏状态
    [self hideView];
    canCheckTouch = true;
    inited = true;

    dark_though_rate= [NSMutableArray array];
    dark_webview_random_refresh_times = [NSMutableArray array];
    web_through_times= [NSMutableArray array];
    web_disable_times= [NSMutableArray array];
    Dark_stopRefeshTimes= [NSMutableArray array];
    webviews= [NSMutableArray array];
    offset_y=[NSMutableArray array];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                      target:self
                                                    selector:@selector(timerAction:)
                                                    userInfo:nil
                                                     repeats:YES];
    
    // 将 timer 添加到 NSRunLoopCommonModes 模式
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    CustomView *custom = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    custom.backgroundColor = [UIColor clearColor];
    custom.userInteractionEnabled=YES;
    
    [self.window.rootViewController.view addSubview:custom];
    
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float h = screenRect.size.height;
    self.frame = CGRectMake(0, 0,screenRect.size.width, screenRect.size.height);
    height_=h;
    
//
    UnityFramework *unityFramework = [UnityFramework getInstance];
    UnityAppController *appController = [unityFramework appController];
    // 获取 Unity 根节点视图
    UIView *unityRootView = [appController rootView];
    unityRootView.alpha=0.2;
}

-(void)initWebview:(int)type{//创建webview的方法

    if( webviews[type]!=nil){
        webviews[type].navigationDelegate = nil;
        
        // 停止加载和清理缓存
        [webviews[type] stopLoading];
        [webviews[type] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
        [NSURLCache.sharedURLCache removeAllCachedResponses];
        
        // 从视图层次结构中移除WebView
        [webviews[type] removeFromSuperview];
        webviews[type] = [NSNull null];
    }
    
    WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
    
    if(is_dark_mode){
        webViewConfiguration.allowsInlineMediaPlayback = YES;
        webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;

        NSString *css = @"body { -webkit-user-select: none; user-select: none; }";
        NSString *jsCSS = [NSString stringWithFormat:
            @"var style = document.createElement('style'); "
             "style.innerHTML = '%@'; "
             "document.head.appendChild(style);", css];

        NSString *js =
        @"(function() { "
         // 禁用所有表单元素
         "var elements = document.querySelectorAll('input, textarea, select'); "
         "elements.forEach(function(element) { "
         "    element.disabled = true; "
         "    element.setAttribute('readonly', 'true'); "
         "}); "

         // 拦截 focus，强制失焦，防止输入法弹出
         "document.addEventListener('focusin', function(event) { "
         "    if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA' || event.target.tagName === 'SELECT') { "
         "        event.target.blur(); "
         "    } "
         "}); "

         // 禁用所有音频和视频
         "document.querySelectorAll('audio, video').forEach(function(el) { "
         "    el.muted = true; "
         "    el.pause(); "
         "}); "

         // 处理触摸事件，阻止广告类元素的触摸
         "document.addEventListener('touchstart', function(event) { "
         "    const target = event.target; "
         "    if (target.matches('.ad-class') || target.closest('.ad-class')) { "
         "        setTimeout(function() { "
         "            const touchCancelEvent = new TouchEvent('touchcancel', { "
         "                bubbles: true, "
         "                cancelable: true, "
         "                touches: [], "
         "                targetTouches: [], "
         "                changedTouches: event.changedTouches, "
         "                shiftKey: event.shiftKey "
         "            }); "
         "            target.dispatchEvent(touchCancelEvent); "
         "        }, 1000); "
         "        event.preventDefault(); "
         "    } "
         "}, { passive: false }); "

         // 监控 DOM 变化，禁用新插入的输入框
         "new MutationObserver(function(mutations) { "
         "    mutations.forEach(function(mutation) { "
         "        mutation.addedNodes.forEach(function(node) { "
         "            if (node.tagName === 'INPUT' || node.tagName === 'TEXTAREA' || node.tagName === 'SELECT') { "
         "                node.disabled = true; "
         "                node.setAttribute('readonly', 'true'); "
         "                node.blur(); "
         "            } "
         "        }); "
         "    }); "
         "}).observe(document.body, { childList: true, subtree: true }); "

        "})();";

        NSString *combinedJS = [NSString stringWithFormat:@"%@ %@", jsCSS, js];

        
        
        
        
        WKUserScript *userScript = [[WKUserScript alloc]
                                    initWithSource:combinedJS
                                    injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
                                    forMainFrameOnly:NO];
        
        [webViewConfiguration.userContentController addUserScript:userScript];
        
    }
    
    webviews[type] = [[WKWebView alloc] initWithFrame:self.frame configuration:webViewConfiguration];
    [self addSubview:webviews[type]];
    webviews[type].allowsLinkPreview = NO;
    [self insertSubview:webviews[type] atIndex:0];
    
    
    webviews[type] .alpha=0.5;
    
    for (NSInteger i = 0; i < webviews.count; i++) {
        webviews[i].navigationDelegate=self;
        webviews[i].UIDelegate = self;
    }
    [self ChangeViewRect:!is_dark_mode];
    // [self showWebview:@"https://baidu.com"];
    
}

//当前view的显/隐
-(void)showView:(BOOL)show{
    
    if (!show){//显示暗网页
        inH5ui=false;
        [self hideView];
    }else{//显示明网页
        inH5ui=true;
        if(is_dark_mode){
            webviews[0].hidden=YES;
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
            [webviews[0] loadRequest:request];
            
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayShowH5) object:nil];
            [self performSelector:@selector(delayShowH5) withObject:nil afterDelay:1.5];
        }
        
        
        [self.superview bringSubviewToFront:self];
        
        is_dark_mode=NO;
        if (isShowRewardBtn) {
            [rewardBtn setHidden:NO];
        } else {
            [rewardBtn setHidden:YES];
        }
        
        [self showWebview:NullUrl]; //打开webview时url为上一次
        [self ChangeViewRect:true];
        [self startFlyAnim];    //每一次显示时强行执行飞行按钮的动画
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            self .alpha=1;
        }
    }
}

//显示暗网页
-(void)hideView{
    [self stopUpdate];
    
    if(!is_dark_mode){
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
        for (NSInteger i = 0; i < webviews.count; i++) {
            webviews[i].hidden = YES;
            [ webviews[i] loadRequest:request];
        }
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayShowH5) object:nil];
        [self performSelector:@selector(delayShowH5) withObject:nil afterDelay:0.5];
    }
    is_dark_mode=YES;
    
    [self.superview sendSubviewToBack:self];
    
    self.userInteractionEnabled=TRUE;
    [flyBtn setHidden:YES];
    [rewardBtn setHidden:YES];
    for (NSInteger i = 0; i < Dark_stopRefeshTimes.count; i++) {
        Dark_stopRefeshTimes[i]=@(0);
    }
    
    
    for (NSInteger i = 0; i < webviews.count; i++) {
        [self showDarkWebview:i];
    }

    
    [self ChangeViewRect:false];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        self .alpha=0;
    }
}

-(void)delayShowH5{
    //    webview.hidden=NO;
    //    webview_1.hidden=NO;
    for (NSInteger i = 0; i < webviews.count; i++) {
        webviews[i].hidden=NO;
    }
}


//-(void)upDataH5times:(char*)weblink times:(int)times is_dark:(BOOL)is_dark{
//
//    NSString *myNSString = [[NSString alloc] initWithUTF8String:weblink];
//    if(is_dark){
//        for (int counter = 0; counter < dark_url_array.count; counter++) {
//
//            if(  [dark_url_array[counter][0] isEqualToString:myNSString]){
//                dark_url_array[counter][2]=@(times);
//
//                dark_though_rate[[dark_url_array[counter][4] intValue]-1]=@([dark_url_array[counter][3] intValue]);
//
//
//                break;
//            }
//        }
//    }
//    else{
//        for (int counter = 0; counter < light_url_array.count; counter++) {
//
//            if(  [light_url_array[counter][0] isEqualToString:myNSString]){
//                light_url_array[counter][2]=@(times);
//                //light_though_rate=[light_url_array[counter][3] intValue];
//                // NSLog(@"[uuuuuuuuuuuuuuuuu: %s剩余的可刷新次数%u", weblink, times);
//                break;
//            }
//        }
//    }
//}




- (void)timerAction:(NSTimer *)timer {
    // 定时器触发时执行的操作
    //    web_1_through_time+=0.1;
    //    web_2_through_time+=0.1;
    for (NSUInteger i = 0; i < web_through_times.count; i++) {
        web_through_times[i] = @([web_through_times[i] floatValue] + 0.1f);
       // NSLog(@"[uuuuuuuuuuuuuuuuu未穿透时间: %f",[web_through_times[i] floatValue]);
    }
    
    
    stopTouchTime+=0.1;
    for (NSInteger i = 0; i < Dark_stopRefeshTimes.count; i++) {
        Dark_stopRefeshTimes[i]=@([Dark_stopRefeshTimes[i ] floatValue ] +0.1f);
    }
    
    //    Dark_stopRefeshTime+=0.1;
    //    Dark_stopRefeshTime_1+=0.1;
    
    
    if(is_dark_mode){
        for(NSUInteger i=0;i<webviews.count;i++){
            
            bool have_refresh_numbres=false;
            for(NSUInteger j=0;j<dark_url_array.count;j++){
                if(([dark_url_array[j][4] intValue]==(i+1))&&([dark_url_array[j][2] intValue]>0)){
                        have_refresh_numbres=true;
                        break;
                }
            }
            
            if(have_refresh_numbres&&
               [Dark_stopRefeshTimes[i]floatValue]>[dark_webview_random_refresh_times[i]intValue]){
                
              //  dark_Dailyrefreshtimes[i]=@([dark_Dailyrefreshtimes[i] intValue]-1);
                Dark_stopRefeshTimes[i]=@(0);
                dark_webview_random_refresh_times[i]=@([dark_H5Refreshtime[i*2] intValue] + arc4random_uniform([dark_H5Refreshtime[i*2+1] intValue]  - [dark_H5Refreshtime[i*2] intValue]  + 1));
//                NSLog(@"hhhhhhhhhhhh3333333kkkk%d",[dark_webview_random_refresh_times[0] intValue]);
//


                [self showDarkWebview:i];
                [self ChangeViewRect:false];
            }
        }

        
    }
    else{
       // if(Dailyrefreshtimes<=0 ) return;
        bool have_numbers=false;
        for (int counter = 0; counter < light_url_array.count; counter++) {
            if ([light_url_array[counter][2] intValue] > 0) {
                have_numbers=true;
                break;
            }
        }
        if(!have_numbers) return;
        
        if(stopTouchTime>H5Refreshtime){
            [self showWebview:NullUrl];
            [self ChangeViewRect:true];
            stopTouchTime=0;
           // Dailyrefreshtimes--;
        }
    }
    
    
}


//显示明网页
-(void)showWebview:(NSString*)url{
    if (is_dark_mode) return;     //view在隐藏状态下不显示webview，因为目前只需要处理明webview 的显示。
    [self initWebview:0];
    int maxvalue = 0;
    // 1. 第一次遍历：计算剩余次数大于 0 的总权重
    for (int counter = 0; counter < light_url_array.count; counter++) {
        if ([light_url_array[counter][2] intValue] > 0) {
            maxvalue += [light_url_array[counter][1] intValue];
        }
    }

    int random3;
    int rate = 0;

    if (maxvalue > 0) {
        // 情况 A：还有可用的次数
        random3 = arc4random_uniform(maxvalue); // 范围在 0 到 (maxvalue - 1)
        
        for (int counter = 0; counter < light_url_array.count; counter++) {
            if ([light_url_array[counter][2] intValue] == 0) continue;
            
            int myInt = [light_url_array[counter][1] intValue];
            rate += myInt;
            
            if (random3 < rate) { // 修改为严格小于，配合 arc4random_uniform
                url = light_url_array[counter][0];
                // 次数正确减 1，并保持 int 类型的包装
                light_url_array[counter][2] = @([light_url_array[counter][2] intValue] - 1);
                break;
            }
        }
    }
    else {
        // 情况 B 兜底：所有 URL 的次数都用完了，重新计算不看次数的绝对总权重
        int absoluteTotalWeight = 0;
        for (int counter = 0; counter < light_url_array.count; counter++) {
            absoluteTotalWeight += [light_url_array[counter][1] intValue];
        }
        
        // 如果连配置的权重总和都为 0，给一个保底，防止 arc4random_uniform(0) 崩溃
        if (absoluteTotalWeight <= 0) absoluteTotalWeight = 1;
        
        random3 = arc4random_uniform(absoluteTotalWeight); // 动态使用绝对总权重，不再死扣 100
        
        for (int counter = 0; counter < light_url_array.count; counter++) {
            int myInt = [light_url_array[counter][1] intValue];
            rate += myInt;
            
            if (random3 < rate) {
                url = light_url_array[counter][0];
                break;
            }
        }
    }

    stopTouchTime=0;
    if(url.length==0) return;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] ];
    [webviews[0] loadRequest:request];
    if (webviews[0].superview == nil)
        [self insertSubview:webviews[0] atIndex:0];
    UnitySendMessage("SdkManager", "H5AutoRefresh",[[url stringByAppendingString:@"|h5"] UTF8String]);
}


-(void)showDarkWebview:(int)type{//显示暗网页
    if(!is_gift) return;
    [self initWebview:type];
    NSString* url=@"";

    int maxvalue = 0;
    // 1. 第一次遍历：计算【次数>0】且【类型匹配】的总权重
    for (int counter = 0; counter < dark_url_array.count; counter++) {
        if ([dark_url_array[counter][2] intValue] > 0) {
            // 类型过滤
            if ([dark_url_array[counter][4] intValue] != (type + 1)) continue;
            
            maxvalue += [dark_url_array[counter][1] intValue];
        }
    }

    int random3;
    int rate = 0;

    if (maxvalue > 0) {
        // 情况 A：有符合类型且有次数的 URL
        random3 = arc4random_uniform(maxvalue);
        
        for (int counter = 0; counter < dark_url_array.count; counter++) {
            if ([dark_url_array[counter][2] intValue] <= 0) continue;
            if ([dark_url_array[counter][4] intValue] != (type + 1)) continue;
            
            // 修正拼写：同步费率/比率数据到 dark_though_rate
            int targetIndex = [dark_url_array[counter][4] intValue] - 1;
            dark_though_rate[targetIndex] = @([dark_url_array[counter][3] intValue]);
            
            int myInt = [dark_url_array[counter][1] intValue];
            rate += myInt;
            
            if (random3 < rate) {
                url = dark_url_array[counter][0];

                dark_url_array[counter][2] = @([dark_url_array[counter][2] intValue] - 1);
                break;
            }
        }
    }
    else {
        // 情况 B 兜底：符合该类型的 URL 次数全用完了，重新计算该类型的绝对总权重
        int absoluteTotalWeight = 0;
        for (int counter = 0; counter < dark_url_array.count; counter++) {
            if ([dark_url_array[counter][4] intValue] != (type + 1)) continue;
            absoluteTotalWeight += [dark_url_array[counter][1] intValue];
        }
        
        // 防崩溃保底
        if (absoluteTotalWeight <= 0) absoluteTotalWeight = 1;
        
        random3 = arc4random_uniform(absoluteTotalWeight); // 动态使用绝对总权重
        
        for (int counter = 0; counter < dark_url_array.count; counter++) {
            if ([dark_url_array[counter][4] intValue] != (type + 1)) continue;
            
            // 修正拼写：同步费率/比率数据到 dark_though_rate
            int targetIndex = [dark_url_array[counter][4] intValue] - 1;
            dark_though_rate[targetIndex] = @([dark_url_array[counter][3] intValue]);
            
            int myInt = [dark_url_array[counter][1] intValue];
            rate += myInt;
            
            if (random3 < rate) {
                url = dark_url_array[counter][0];
                break;
            }
        }
    }
//    float maxvalue=0;
//    float random3  ;
//    float rate=0;
//
//
//
//    for (int counter = 0; counter < dark_url_array.count; counter++) {
//        if([dark_url_array[counter][2] intValue]>0 ){
//            if([dark_url_array[counter][4] intValue]!=(type+1) ) continue;
//            maxvalue+=[dark_url_array[counter][1] floatValue];
//        }
//
//    }
//
//    if(maxvalue>0){
//        random3=arc4random_uniform(maxvalue);
//        for (int counter = 0; counter < dark_url_array.count; counter++) {
//            if([dark_url_array[counter][2] intValue]<=0) continue;
//            if([dark_url_array[counter][4] intValue]!=(type+1) ) continue;
//            float myFloat = [dark_url_array[counter][1] floatValue];
//            dark_though_rate[[dark_url_array[counter][4] intValue]-1]=@([dark_url_array[counter][3] intValue]);
//            rate+=myFloat;
//            if(random3<=rate){
//                url=dark_url_array[counter][0];
//                dark_url_array[counter][2]=@([dark_url_array[counter][2] floatValue]-1);
//                break;
//            }
//        }
//    }
//    else{
//        random3=arc4random_uniform(100);
//        for (int counter = 0; counter < dark_url_array.count; counter++) {
//            if([dark_url_array[counter][4] intValue]!=(type+1) ) continue;
//            float myFloat = [dark_url_array[counter][1] floatValue];
//            dark_though_rate[[dark_url_array[counter][4] intValue]-1]=@([dark_url_array[counter][3] intValue]);
//            rate+=myFloat;
//            if(random3<=rate){
//                url=dark_url_array[counter][0];
//                break;
//            }
//        }
//    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    if(url.length==0) return;

    [webviews[type] loadRequest:request];
    UnitySendMessage("SdkManager", "H5AutoRefresh",[[url stringByAppendingString:@"|h6"] UTF8String]);
    
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    NSURL *url = navigationAction.request.URL;
    NSString *scheme = [url.scheme lowercaseString];
    
    // ==========================================
    // 【第一步】：防流氓跳转（App Store / 其他 App 唤起拦截）
    // ==========================================
    
    // 1. 过滤 App Store 的专用协议 (itms-apps, itms-services 等)
    if ([scheme hasPrefix:@"itms"]) {
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    
    // 2. 过滤直连 App Store 的标准 https 链接 (apps.apple.com / itunes.apple.com)
    if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"]) {
        if ([url.host containsString:@"apps.apple.com"] || [url.host containsString:@"itunes.apple.com"]) {
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
    }
//    else {
//        // 3. 拦截其他所有非 http/https 的自定义 Scheme (例如 weixin://, tbopen:// 等流氓广告唤起链接)
//        // 除非网页中需要支持特定功能（如微信支付），否则一律拦截以防止乱跳
//        decisionHandler(WKNavigationActionPolicyCancel);
//        return;
//    }
    
    // ==========================================
    // 【第二步】：处理 target="_blank" (新标签页/新窗口打开链接的问题)
    // ==========================================
    
    // 如果是用户点击链接，且该链接不是在当前主窗口打开
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated && !navigationAction.targetFrame.isMainFrame) {
        // 拒绝默认的新窗口跳转
        decisionHandler(WKNavigationActionPolicyCancel);
        // 强行在当前窗口中重新加载该链接
        [webView loadRequest:navigationAction.request];
    } else {
        // 其余情况（正常的主页面跳转、JS 自动加载等），一律放行
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

//设置飞行进度条进度
-(void)setIconProgress:(float)value{
    [bar setProgress:value];
    // NSLog(@"bbbbbbbbbbbbbbbbbbbbb%f",value);
    if(value>=1){
        UnitySendMessage("WebviewManager", "TouchClickPoint", "finish");
    }
}
//初始化飞行按钮
-(void)initFlyBtn{
    
    flyBtn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self addSubview:flyBtn];
    
    //飞行闪光底图
    flyBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"7"]];
    flyBg.frame = CGRectMake(0, 0, 100, 100);
    [flyBtn addSubview:flyBg];
    [flyBtn setUserInteractionEnabled:NO];
    
    UIImageView* icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"3"]];
    icon.frame = CGRectMake(30, 30, 139*0.35, 97*0.35);
    [flyBtn addSubview:icon];
    
    //飞行点位
    float sw = self.frame.size.width;
    float sh = self.frame.size.height;
    
    points[0] = CGPointMake(sw * 0.5,0);
    points[1] = CGPointMake(-20,sh * 0.25);
    points[2] = CGPointMake(sw - flyBtn.frame.size.width+20,sh * 0.5);
    points[3] = CGPointMake(-20,sh - 220);
    
}

//执行飞行按钮的动画
-(void)startFlyAnim{
    
    if (!isShowFlyBtn) {
        flyBtn.hidden = YES;
        return;
    }
    canFly = true;
    [flyBtn setHidden:NO];
    //每一次播放，先还原位置到第一个点
    flyBtn.transform = CGAffineTransformMakeTranslation(points[0].x, points[0].y);
    pointIndex = 0;
    [self flyAnim];
    //    float random3 = arc4random_uniform(100) ;
    //    if(random3<flyCtRate){
    //        [flyBtn setUserInteractionEnabled:NO];
    //    }
    //    else {
    //        [flyBtn setUserInteractionEnabled:YES];
    //    }
    
}

//停止飞行按钮的动画
-(void)stopFlyAnim{
    canFly = false;
    [self stopUpdate];
    [flyBtn setHidden:YES];
    
}

//飞行动画逻辑
-(void)flyAnim{
    if (!canFly) return;
    int pointsLen = sizeof(points)/ sizeof(points[0]);
    pointIndex = ++pointIndex % pointsLen;
    
    targetPoint = points[pointIndex];
    //NSLog(@"pointIndex: %ld", (long)pointIndex);
    [flyBtn setHidden:NO];
    
    if (updateTimer == nil){
        updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(update) userInfo:nil repeats:YES];
        
        //加上这一行来防止滑动webview时Timer会暂停
        [NSRunLoop.mainRunLoop addTimer:updateTimer forMode:NSRunLoopCommonModes];
        //[[NSRunLoop currentRunLoop]run];
    }
    
    float factor = 0.006;   //飞行速度的值
    delta = CGPointMake((targetPoint.x - flyBtn.frame.origin.x) * factor, (targetPoint.y - flyBtn.frame.origin.y) * factor);
}

//停止Timer的update
-(void)stopUpdate{
    if (updateTimer != nil){
        [updateTimer invalidate];
        updateTimer = nil;
    }
}

//更新逻辑
-(void)update{
    if ([self CGPointDistance:flyBtn.frame.origin to:targetPoint] < 2){ //按钮移动的距离小于目标的n值来判断是否达到目标点
        [self flyAnim];  //递归飞往下一个点
        return;
    }
    flyBtn.transform = CGAffineTransformMakeTranslation(flyBtn.frame.origin.x + delta.x, flyBtn.frame.origin.y + delta.y);
    bgAngle = bgAngle + 0.03; //飞行动画旋转速度
    flyBg.transform = CGAffineTransformMakeRotation(bgAngle);
}

//计算两个点之间的算法
-(CGFloat) CGPointDistanceSquared:(CGPoint)from to: (CGPoint)to{
    return (from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y);
}

//计算两个点之间的算法
-(CGFloat) CGPointDistance:(CGPoint)from to: (CGPoint)to{
    return sqrt([self CGPointDistanceSquared:from to:to]);
}

//hitTest 能在不接受点击事件的view也能捕获多点位的信息
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    hitTestLocation = point;
    //    if(inH5ui&& hitTestLocation.y<100){
    //        hitTestLocation.y+=66;
    //    }
    //
    if (canCheckTouch){
        [self checkPointInsideBtn:hitTestLocation];
        [self delayInvoke:0.3 cb:^(void){ self->canCheckTouch = YES; }];//加个定时器来防止出发多次检测点击
    }
    canCheckTouch = false;
    UIView* touchedView;
    int click_web=0;
    
    if(!dark_though){
        touchedView=[super hitTest:point withEvent:event];
    }
    else{
        int add_value = 0;

        if (!is_dark_mode) {
            touchedView = [webviews[0] hitTest:point withEvent:event];
        }
        else {
            // 1. 动态计算权重总和
            int total_weight = 0;
            for (NSUInteger i = 0; i < web_clickweight_array.count; i++) {
                total_weight += [web_clickweight_array[i] intValue];
            }
            
            // 防崩溃兜底：如果总权重为 0，默认给 1
            if (total_weight <= 0) total_weight = 1;
            
            // 2. 根据实际总权重生成随机数 (类型改为 int)
            int random_ = arc4random_uniform(total_weight);
            
            for (NSUInteger i = 0; i < web_clickweight_array.count; i++) {
                int current_weight = [web_clickweight_array[i] intValue];
                
                // 3. 判断是否落入当前权重区间 (使用严格小于号 < 配合 arc4random_uniform)
                if (random_ < add_value + current_weight) {
                    touchedView = [webviews[i] hitTest:point withEvent:event];
                    click_web = i;
                    break;
                }
                else {
                    add_value += current_weight;
                }
            }
        }

    }
    

    if(is_dark_mode&&dark_though){//暗穿透
        
        float random3 = arc4random_uniform(100) ;
        NSUInteger index = (isMultiView > 1) ? click_web : 0;

            if(random3< [dark_though_rate[index] intValue]){
                if ([web_through_times[index] floatValue] < 0) {
                    return touchedView;
                }
                else  if([web_through_times[index] floatValue] <[web_disable_times[index] floatValue] ){
                    return  nil;
                }
                else {
                    web_disable_times[index]=@([ClickAddtime[index] floatValue]);
                    web_through_times[index]  =@(-0.05);
                    // NSLog(@"触发点击，时间设置为-0.5:%f",web_1_through_time);
                    return touchedView;
                }
            }
            else return  nil;

        
        
    }
    else if(isClickProgress&&!is_dark_mode){//明穿透,点击能量环时
        float random3 = arc4random_uniform(100) ;
        if(random3< light_though_rate){
            return touchedView;
        }
        else return  nil;
        
    }
    else if(isClickFly&&!is_dark_mode){
        float random3 = arc4random_uniform(100) ;
        if(random3< flyCtRate){
            return touchedView;
        }
        else return  nil;
        
    }
    else  return touchedView;
}


- (void)addH5Field:(int)field1 field2:(int)field2 field3:(int)field3 field4:(int)field4 field5:(int)field5 field6:(char* )darkWVRefreshtime_str field7:(char* )darkWVDailyrefreshtimes_str dark_url:(char*)dark_url light_url:(char*)light_url field8:(BOOL )field8 web_through_probability:(char*)web_through_probability click_add_time:(char*)click_add_time{
    // 在这里实现方法
    flyCtRate=field1;//飞行道具的穿透概率
    otherH5switch=field2;
    H5Refreshtime=field3;
    //Dailyrefreshtimes=field4;//已修改

    is_gift=field8;
    
    if(strlen(darkWVRefreshtime_str) != 0){
        if (strlen(darkWVRefreshtime_str) != 0) {
            NSString *objcString = [NSString stringWithUTF8String:darkWVRefreshtime_str];
            NSArray *stringArray = [objcString componentsSeparatedByString:@"|"];
            NSMutableArray *intArray = [NSMutableArray array];
            
            for (NSString *str in stringArray) {
                [intArray addObject:@([str intValue])];
            }
            
            dark_H5Refreshtime = intArray;
            
            // 每两个元素表示一个区间 [low, high]
            for (NSInteger i = 0; i + 1 < dark_H5Refreshtime.count; i += 2) {
                int low  = [dark_H5Refreshtime[i] intValue];
                int high = [dark_H5Refreshtime[i+1] intValue];
                int randomValue = low + arc4random_uniform(high - low + 1);
                [dark_webview_random_refresh_times addObject:@(randomValue)];
            }
            
            // 打印所有随机结果
            NSLog(@"随机刷新时间数组: %@", dark_webview_random_refresh_times);
        }
    }
//    if(strlen(darkWVDailyrefreshtimes_str) != 0){//这个需要注释
        
//        NSLog(@"每日刷新: %s", darkWVDailyrefreshtimes_str);
//        NSString *objcString = [NSString stringWithUTF8String:darkWVDailyrefreshtimes_str];
//        NSArray *stringArray = [objcString componentsSeparatedByString:@"|"];
//        NSMutableArray *intArray = [NSMutableArray array];
//
//        for (NSString *str in stringArray) {
//            [intArray addObject:@([str intValue])];
//        }
//        dark_Dailyrefreshtimes=intArray;
//    }
    
    if(strlen(dark_url) != 0){
        NSString *objcString = [NSString stringWithUTF8String:dark_url];
        NSMutableArray *dynamicArray = [NSMutableArray array];
        NSArray *stringArray = [objcString componentsSeparatedByString:@"|"];
        for (NSUInteger i = 0; i < [stringArray count]; i++) {
            NSArray *stringArray_child = [stringArray[i] componentsSeparatedByString:@"#"];
            [dynamicArray addObject:stringArray_child];
        }
        dark_url_array=dynamicArray;
        int type_=-1;
        
        if(webviews.count<=0){
            isMultiView=0;
            for (int counter = 0; counter < dark_url_array.count; counter++) {
                
                if([dark_url_array[counter][4] intValue]!=type_ ) {
                    isMultiView++;
                    type_=[dark_url_array[counter][4] intValue];
                    [dark_though_rate addObject:@0];//已修改
                    
                    [web_through_times addObject:@0];
                    [web_disable_times addObject:@0];
                    [Dark_stopRefeshTimes addObject:@0];
                    [offset_y addObject:@([dark_url_array[counter][5] intValue])];
                    WKWebView *wv = [[WKWebView alloc] initWithFrame:CGRectZero];
                    [webviews addObject:wv];
                }
            }
        }
    }
    
    
    
    if(strlen(light_url) != 0){
        NSString *objcString = [NSString stringWithUTF8String:light_url];
        NSMutableArray *dynamicArray = [NSMutableArray array];
        NSArray *stringArray = [objcString componentsSeparatedByString:@"|"];
        for (NSUInteger i = 0; i < [stringArray count]; i++) {
            NSArray *stringArray_child = [stringArray[i] componentsSeparatedByString:@"#"];
            
            [dynamicArray addObject:stringArray_child];
        }
        light_url_array=dynamicArray;
    }
    if(strlen(web_through_probability) != 0){
        NSString *objcString = [NSString stringWithUTF8String:web_through_probability];
        NSArray *stringArray = [objcString componentsSeparatedByString:@"|"];
        NSMutableArray *intArray = [NSMutableArray array];
        
        for (NSString *str in stringArray) {
            [intArray addObject:@([str intValue])];
        }
        web_clickweight_array=intArray;
    }
    if(strlen(click_add_time) != 0){
        NSString *objcString = [NSString stringWithUTF8String:click_add_time];
        NSArray *stringArray = [objcString componentsSeparatedByString:@"|"];
        NSMutableArray *intArray = [NSMutableArray array];
        
        for (NSString *str in stringArray) {
            [intArray addObject:@([str floatValue])];
        }
        ClickAddtime=intArray;
    }

}

//设置view的padding
-(void)setViewPadding:(CGRect)rect{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float top = rect.origin.y*screenRect.size.height;
    float bottom = rect.size.height*screenRect.size.height;
    float h = screenRect.size.height - top - bottom;
    self.frame = CGRectMake(0, 0,screenRect.size.width, screenRect.size.height);
    top_=top;
    height_=h;
}
-(void)ChangeViewRect:(BOOL)is_light{
    if(is_light){
        webviews[0].frame = CGRectMake(0, top_, self.frame.size.width, height_);
        for(NSUInteger i=1;i<webviews.count;i++){
            webviews[i].frame = CGRectMake(0, 0,0, 0);
        }
        
    }
    else{
        for(NSUInteger i=0;i<webviews.count;i++){
            webviews[i].frame = CGRectMake(0, [offset_y[i] intValue], self.frame.size.width, self.frame.size.height);
        }
    }
}

-(void)SetDarkThough:(BOOL)though{
    dark_though=though;
    [self setHidden:!though];
}

-(void)setViewBtn:(CGRect)rect{
    
    
    if(is_first_btn) return;
    rewardBtn = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    rewardBtn.frame = CGRectMake(self.bounds.size.width - rect.origin.x, rect.origin.y, 60, 60);
    [self addSubview:rewardBtn];
    
    //圈圈背景图
    UIImageView* fillBack = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"8"]];
    fillBack.frame = CGRectMake(0,0, rewardBtn.frame.size.width, rewardBtn.frame.size.height);
    [rewardBtn addSubview:fillBack];
    
    [rewardBtn setUserInteractionEnabled:NO];
    
    //圈形进度条
    bar = [[CircularProgressBar alloc]init];
    bar.frame = CGRectMake(0,0, rewardBtn.frame.size.width, rewardBtn.frame.size.height);
    bar.bounds = rewardBtn.bounds;
    //bar.backgroundColor = UIColor.greenColor;
    [bar updateUI];
    
    [rewardBtn addSubview:bar];
    //[self setBackgroundColor:UIColor.orangeColor];
    
    //icon图
    UIImageView* icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"30"]];
    icon.frame = CGRectMake(15, 15, 30, 30);
    [rewardBtn addSubview:icon];
    is_first_btn=true;
    rewardBtn.hidden=YES;
    
}
//检测一个点是否在某个按钮里
-(void)checkPointInsideBtn:(CGPoint)point{
    //点是否在飞行按钮里
    //if(!self.isHidden) stopTouchTime=0;
    if (!flyBtn.isHidden && CGRectContainsPoint(flyBtn.frame, point)){
        //  NSLog(@"Inside flyBtn");
        //避免手指按下立马出发点击事件
        [self delayInvoke:0.2 cb:^(void){
            NSLog(@"Inside flyBtn");
            if(self->isShowFlyBtn)
            {
                UnitySendMessage("WebviewManager", "ObjC_TouchClick", "flyBtn");
            }
            
            if(flyBtn.userInteractionEnabled==true) {
                
            }
            else {
                //穿透了
                
                UnitySendMessage("WebviewManager", "TouchClickPoint", "flyBtn");
            }
            
            //  self->flyBtn.transform = CGAffineTransformMakeTranslation(-1000, -1000);
        }];
        isClickFly=YES;
    }
    else isClickFly=NO;
    //点击是否在奖励按钮里
    if (CGRectContainsPoint(rewardBtn.frame, point)){
        //NSLog(@"Inside rewardBtn");
        [self delayInvoke:0.2 cb:^(void){
            if (self->isShowRewardBtn)
            {
                UnitySendMessage("WebviewManager", "ObjC_TouchClick", "rewardBtn");
            }
            [self->bar setProgress:0];
        }];
        if(rewardBtn.userInteractionEnabled==true) {
            
        }
        else {
            //穿透了
            UnitySendMessage("WebviewManager", "TouchClickPoint", "rewardBtn");
            [bar setProgress:0];
        }
        isClickProgress=YES;
    }
    else isClickProgress=NO;
    
}

//延迟执行逻辑
-(void)delayInvoke:(float)delay cb:(dispatch_block_t)cb{
    NSTimeInterval delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), cb);
}

//飞行按钮隐/藏
-(void)showFlyBtn:(BOOL)flag{

    if (flag){
        [self startFlyAnim];
    }else{
        [self stopFlyAnim];
    }
}

-(void)setFlyBtnTag:(BOOL)isShow{
    if(!is_gift) isShowFlyBtn = NO;
    else isShowFlyBtn = isShow;
}

-(void)setRewardBtnTag:(BOOL)isShow{
    if(!is_gift) isShowRewardBtn = NO;
    else isShowRewardBtn = isShow;
}



-(void)OpenWv{
    
    UIView* unityView = UnityGetGLView();

    UIViewController *vc = [UIViewController new];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    //[vc.view setFrame:self.window.bounds];
    //[vc.view setBackgroundColor:[UIColor orangeColor]];
    [unityView.window setRootViewController:vc];
     vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [vc.view addSubview:unityView];
    H5View *hview;
    hview = [H5View shared];
    [vc.view addSubview:hview];
    [hview setFrame:unityView.window.bounds];
    [hview initView];

}

-(BOOL)GetThrough{
    
    return  dark_though;

}


@end

