//
//  SecondStartPageViewController.m
//  Open
//
//  Created by 雷俊博 on 2021/8/19.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "SecondStartPageViewController.h"
#import "UIImageView+WebCache.h"

@interface SecondStartPageViewController ()

@property (nonatomic, strong)UIImageView*placeImage;


@property (nonatomic, strong)UIButton* skipBtn;

@property (nonatomic, strong)KKLogBannerModel* rModel;

@end

@implementation SecondStartPageViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.placeImage];
}


- (void)loadMyAdData
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    app_Version = [app_Version stringByReplacingOccurrencesOfString:@"." withString:@""];
    
    NSDictionary *params = @{
        @"userId":@([getPeopleID PeopleID]),
        @"telephone":[EnercryTool encryptUseDES:[getPeopleID PeopleIphoneNmber] key:DESKEY],
        @"fromApp":@"kksh",
        @"versionNo":@(app_Version.integerValue),
        @"fromModel":@"2",
        @"category":@"6002"
    };
    
    NSString * url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipKKSBConfig],KKLogBannerRequest];
    
    [NetworkRequest SBAuthPost:url Params:params Success:^(id _Nonnull objc) {
        
        NSLog(@"objc:%@",objc);
        
        if ([objc isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = (NSDictionary *)objc;
            NSString *code = [NSString stringWithFormat:@"%@",objc[@"code"]];
            if ([code isEqualToString:@"6001"]) {
                
                if ([dic[@"result"] isKindOfClass:[NSArray class]]) {
                    
                    NSArray *dataArray = [KKLogBannerModel mj_objectArrayWithKeyValuesArray:dic[@"result"]];
                    if (dataArray.count > 0) {
                        
                        KKLogBannerModel*smodel = dataArray[0];
                        self.rModel = smodel;
                        
                        if ([smodel.eventflag isEqualToString:@"105"]) {
                            
                            if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_ShowWithModel:vc:)]) {
                                [self.delegate secondStartPage_ShowWithModel:self.rModel vc:self];
                            }
                            
                            [self performSelector:@selector(removeLun) withObject:nil afterDelay:5.0];
                            return;
                        }
                        
                        [self.placeImage sd_setImageWithURL:[NSURL URLWithString:smodel.imagepath] placeholderImage:[UIImage imageNamed:[self getCurrentLaunchImageNameForOrientation:UIInterfaceOrientationPortrait]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            if (error) {
                                if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_Fail:)]) {
                                    [self.delegate secondStartPage_Fail:self];
                                }
                            }else{
                                
                                self.placeImage.contentMode =  UIViewContentModeScaleAspectFill;
                                
                                
                                self.placeImage.image = image;
                                
                                if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_ShowWithModel:vc:)]) {
                                    [self.delegate secondStartPage_ShowWithModel:self.rModel vc:self];
                                }
                                
                                self.placeImage.userInteractionEnabled = YES;
                                [self.view addSubview:self.skipBtn];
                                UITapGestureRecognizer*tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
                                [self.placeImage addGestureRecognizer:tap];
                                
                                NSLog(@"1234:%@",[NSDate date]);
                               [self performSelector:@selector(removeLun) withObject:nil afterDelay:5.0];
                                
                            }
                        }];
                        return;
                        
                        
                    }
                    
                }
                
            }
            
            
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_Fail:)]) {
            [self.delegate secondStartPage_Fail:self];
        }
        
        
    } Failure:^(NSError* error) {
        NSLog(@"error:%@",error);
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_Fail:)]) {
            [self.delegate secondStartPage_Fail:self];
        }
    }];
}

-(void)setUIViewBackgound:(UIView *)uiview name:(NSString *)name {
    
    //UIGraphicsBeginImageContext(uiview.frame.size);
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:uiview.frame.size];
    [[UIImage imageNamed:name] drawInRect:uiview.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.placeImage.backgroundColor = [UIColor colorWithPatternImage:image];
}


-(void)removeLun
{
    NSLog(@"4567:%@",[NSDate date]);
    if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_Fail:)]) {
        [self.delegate secondStartPage_Fail:self];
    }
}


- (void)dismissInterimView
{
    [UIView animateWithDuration:0.5 animations:^{
        //_placeImage.alpha = 0;
    } completion:^(BOOL finished) {
        //[_placeImage removeFromSuperview];
    }];
}

#pragma mark - event response
- (void)tap
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(secondStartPage_ClickWithModel:vc:)]) {
        [self.delegate secondStartPage_ClickWithModel:self.rModel vc:self];
    }
}

-(NSString *)getCurrentLaunchImageNameForOrientation:(UIInterfaceOrientation)orientation{
    NSString *currentImageName = nil;

    CGSize viewSize = self.view.bounds.size;
    NSString* viewOrientation = @"Portrait";

    if(UIInterfaceOrientationIsLandscape(orientation)){
        viewSize = CGSizeMake(viewSize.height, viewSize.width);
        viewOrientation = @"Landscape";
    }
    
    NSArray *imageDicts = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary * dic in imageDicts) {
        CGSize imageSize = CGSizeFromString(dic[@"UILaunchImageSize"]);　//将字符串转换为Size
        NSString *orientation = dic[@"UILaunchImageOrientation"];　//取得方向
        if(CGSizeEqualToSize(viewSize, imageSize) && [orientation isEqualToString:viewOrientation]){
            currentImageName = dic[@"UILaunchImageName"];
        }
    }
    
    return currentImageName;
}


#pragma mark - getters and setters
- (UIImageView *)placeImage
{
    if (!_placeImage) {
        _placeImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _placeImage.contentMode =  UIViewContentModeScaleAspectFit;
        _placeImage.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _placeImage.image = [UIImage imageNamed:[self getCurrentLaunchImageNameForOrientation:UIInterfaceOrientationPortrait]];
        //[self setUIViewBackgound:_placeImage name:@"tm22"];
    }
    return _placeImage;
}

- (UIButton *)skipBtn
{
    if (!_skipBtn) {
        _skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_skipBtn setTitle:@"跳过>>" forState:UIControlStateNormal];
        [_skipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_skipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        _skipBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        CGRect f = [UIScreen mainScreen].bounds;
        _skipBtn.frame = CGRectMake(f.size.width - 70, 30, 60, 30);
        _skipBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        _skipBtn.layer.borderWidth = 1.0f;
        _skipBtn.layer.cornerRadius = 3.0f;
        _skipBtn.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.3];
        [_skipBtn addTarget:self action:@selector(removeLun) forControlEvents:UIControlEventTouchUpInside];
    }
    return _skipBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
