//
//  FloatingWindow.m
//  AIIM
//
//  Created by gan on 2025/7/9.
//

#import <Foundation/Foundation.h>
#import "FloatingWindow.h"
#import "APPLockView.h"
#import "FloatJSWindow.h"
#import "AppDelegate.h"

@interface FloatingWindow ()

@property (nonatomic, strong) APPLockView *lockView;
@property (nonatomic, strong) UIView *JSWindow;
@property (nonatomic, assign) CGRect innitframe;
@property (nonatomic, strong) UIButton *floadBT;
@property (nonatomic, strong) UIView *floadNotView;
@end

@implementation FloatingWindow

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _innitframe = frame;
        self.windowLevel = UIWindowLevelAlert + 1;
        self.backgroundColor = [UIColor clearColor];
        self.rootViewController = [UIViewController new];
        
    }
    return self;
}

- (void)show:(NSInteger)type{
    self.hidden = NO;
    [self makeKeyAndVisible];
    if(type==1){
        [self initlockView];
    }
  
}

- (void)hide {
    self.hidden = YES;
}

-(void)initlockView{
    if(_JSWindow){
        _JSWindow.hidden=YES;
    }
    if(_lockView){
        _lockView.hidden=NO;
        return;
    }
    else{
        weakSelf(self);
        _lockView=[[APPLockView alloc] initWithFrame:self.frame];
        _lockView.onShide = ^{
            [weakself hide];
        };
        [self addSubview:_lockView];
    }
}

- (void)showView:(UIView *)view{
    self.hidden = NO;
    if(_lockView){
        _lockView.hidden=YES;
    }
    if(_JSWindow){
        _JSWindow.hidden=NO;
        NSLog(@"_JSWindow.hidden=NO");
        return;
    }
    else{
        _JSWindow=view;
        NSLog(@"__JSWindow=view");
        [self addSubview:_JSWindow];
    }
}

-(void)viewInFload:(NSInteger)type{
    if(type==0){
        self.frame=_innitframe;
        _floadNotView.hidden = YES;
        if(_JSWindow){
            _JSWindow.hidden=NO;
        }
    }
    else if(type==1 || type==2){
        if(_JSWindow){
            _JSWindow.hidden=YES;
        }
        if(_lockView){
            _lockView.hidden=YES;
        }
        [self initfloadNotView:type];
        self.frame =CGRectMake(100, 180, 70, 70);
    }
}

-(void)floadBTsel{
    NSLog(@"floadBTsel----------");
    [self viewInFload:0];
    UIWindow *keyWindow = [UIApplication sharedApplication].delegate.window;
    [keyWindow endEditing:YES];
}

-(void)initfloadNotView:(NSInteger)type{
    NSString *icon = @"Cyuyinth";
    if (type==2) {
        icon = @"Cshipinth";
    }
    
    if(_floadNotView){
        [_floadBT setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        _floadNotView.hidden=NO;
        return;
    }
    else{
        _floadNotView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
        _floadNotView.backgroundColor = globalColor(GCTypeOrangeR);
        _floadNotView.layer.cornerRadius = 8.0f;
        [self addSubview:_floadNotView];
        _floadBT = [UIButton buttonWithType:UIButtonTypeCustom];
        [_floadBT setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        [_floadNotView addSubview:_floadBT];
        [_floadBT addTarget:self action:@selector(floadBTsel) forControlEvents:UIControlEventTouchUpInside];
        [_floadBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(50, 50));
            make.top.mas_equalTo(10);
            make.left.mas_equalTo(10);
        }];
    }
}




@end
