//
//  CustomView.m
//  UnityFramework
//
//  Created by Mac on 2024/1/19.
//

 #import "CustomView.h"
#import "H5View.h"
 @implementation CustomView
 {
//     WKWebView *webview;
//     UIView* unityView;
//     //ViewCfg config;
//     CGPoint hitTestLocation;
//     BOOL isTouchWebView;
//     BOOL isWebviewOpen;
//     BOOL canCT;
     int  test;
 }
 +(id) shared{
     static CustomView *view = nil;
     static dispatch_once_t onceToken;

     dispatch_once(&onceToken, ^{view = [[self alloc] init];});

     return view;
 }

 -(void)initView{
     // Initialize a WKWebViewConfiguration object.

    
 //    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    // [unityView addGestureRecognizer:gest];
   //  [gest setDelegate:self];
 //    CustomGestureDelegate *deleg = [[CustomGestureDelegate alloc] init];
 //    [gest setDelegate:deleg];
 //    gest.cancelsTouchesInView = false; // to recieve touchesBegan callback even if gesture is recognized
 //    gest.delaysTouchesBegan = false; // to not delay touchesBegan callback
 //    gest.delaysTouchesEnded = false; // to not delay touchesEnded callback
 //    CustomGestureDelegate *deleg = [[CustomGestureDelegate alloc] init];
 //    [gest setDelegate:deleg];
 //    NSTimeInterval delayInSeconds = 7;
 //    dispatch_time_t popTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
 //    dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
 //
 //        [self loop];
 //    });
    // [self setWebviewPadding:CGRectMake(0, 120, 0, 180)];
 }


 //-(void)setConfig:(ViewCfg)cfg{
 //    config = cfg;
 //
 //    webview.frame = config.rect;
 //   // webview.frame = CGRectMake(0, 100, self.bounds.size.width, 500);
 //    //canCT = YES;
 //}



// -(void)handleTap:(UITapGestureRecognizer*) gest{
//     CGPoint pt = [gest locationInView:webview];
//   //  NSLog(@"GestureRecognizer x:%f, y:%f", pt.x, pt.y);
// //    if (gest.state == UIGestureRecognizerStateBegan){
// //        NSLog(@"Begin x:%f, y:%f", pt.x, pt.y);
// //    }
//     if (gest.state == UIGestureRecognizerStateEnded){
//         NSLog(@"GestureRecognizer End x:%f, y:%f", pt.x, pt.y);
//     }
// }
// //- (void) touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event{
// //    UITouch *touch = [[event allTouches] anyObject];
// //
// //    CGPoint location = [touch locationInView:self];
// //    beginTouches = touches;
// //    beginEvent = event;
// //    NSLog(@"Touch Began CustomView");
// //}
// //
// //- (void) touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event{
// //    UITouch *touch = [[event allTouches] anyObject];
// //
// //    CGPoint location = [touch locationInView:self];
// //    beginTouches = touches;
// //    beginEvent = event;
// //    NSLog(@"Touch Ended CustomView");
// //}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    //UIView* touchedView = [super hitTest:point withEvent:event];
//    if([[H5View shared] isDarkThough]){
//    NSLog(@"Point coordinates: x = %f, y = %f", point.x, point.y);
//    NSLog(@"Point coordinates: max = %f,",self.bounds.size.width);
    UIView* testview = [ [H5View shared]hitTest:point withEvent:event];
        return testview;
//    }
//    else 
     //   return  touchedView;
}

// -(void)showWebview:(NSString*)url{
//    // NSLog(@"Native showWebview: %@", url);
//     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
//     [webview loadRequest:request];
//     if (webview.superview == nil)
//         [self insertSubview:webview atIndex:0];
//     isWebviewOpen = YES;
// }

// -(void)hideWebview{
//    // NSLog(@"Native hideWebview");
//     [webview removeFromSuperview];
//     isWebviewOpen = NO;
// }

// -(void)scrollWebview:(CGPoint)offset{
//     CGPoint pt = [webview.scrollView contentOffset];
//     if (offset.y < 0 && pt.y <= 0) return;
//     float val = pt.y + offset.y*0.5;
//     if (val<0) val = 0;
//     [webview.scrollView setContentOffset:CGPointMake(0, val)];
// }

// -(void)setTouchWebview:(BOOL)flag forceCT:(BOOL)force{
//     if (!isWebviewOpen) return;
//     isTouchWebView = flag;
//     //unityView.userInteractionEnabled = !flag;
    
//     if (canCT || force){
//         if (!CGRectContainsPoint(webview.bounds, hitTestLocation))
//             return;
//         [self doCT];
//     }
// }

// -(void)tapWithPoint:(CGPoint)zspoint{
//     [Helpers helpAt:1 AtPoint:zspoint withTouchPhase:UITouchPhaseBegan];
//     [Helpers helpAt:1 AtPoint:zspoint withTouchPhase:UITouchPhaseEnded];
// }


// -(void)enableCT:(BOOL)flag{
//    // NSLog(@"Native enableCT: %d",flag);
//     canCT = flag;
// }

// -(void)doCT{
//     NSTimeInterval delayInSeconds = 0.1;
//     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
//     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//       //  NSLog(@"CT x:%f, y:%f", self->hitTestLocation.x, self->hitTestLocation.y);
//         self->unityView.userInteractionEnabled = false;
//         [self tapWithPoint:self->hitTestLocation];
//         self->unityView.userInteractionEnabled = true;
//     });
// }

 @end
