//
//  StartPageViewController.m
//  Open
//
//  Created by 雷俊博 on 2020/9/3.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

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

@interface StartPageViewController ()

@property (nonatomic, strong)UIImageView*placeImage;


@property (nonatomic, strong)UIButton* skipBtn;

@property (nonatomic, strong)homePageNewsResultModel* rModel;

@property (nonatomic, strong)NSString* mPopImg;
@property (nonatomic, strong)NSString* mEventParam;
@property (nonatomic, strong)NSString* mEventFlag;
@end

@implementation StartPageViewController
- (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];
    
    self.placeImage.userInteractionEnabled = YES;
    [self.view addSubview:self.skipBtn];
    
    UITapGestureRecognizer*tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self.placeImage addGestureRecognizer:tap];
}

- (void)loadMyAdData{
    
    NSDictionary *params = @{@"deviceId":[Tools getDeviceUUID],
                             @"terminalType":@"2",
                             @"sourceApp":@"kksh",
                             @"version":[Tools getVersionCode]
    };
    [NetworkRequest Post12:@"https://web.kanface.com:444/pop/active/user/start/app" Params:params Success:^(id _Nonnull object) {
        NSString* state = [NSString stringWithFormat:@"%@",object[@"state"]];
        if ([state isEqualToString:@"200"]) {
            NSDictionary *splashResult = object[@"result"];
            if (splashResult!=nil) {
                NSDictionary *popinfoResult=splashResult[@"popUpInfo"];
                if (popinfoResult!=nil&&![popinfoResult isEqual:[NSNull null]]) {
                    self.mPopImg=popinfoResult[@"popImg"];
                    self.mEventParam=popinfoResult[@"eventParam"];
                    self.mEventFlag=popinfoResult[@"eventFlag"];
                    
                    [self.placeImage sd_setImageWithURL:[NSURL URLWithString:self.mPopImg]];
                    [self.placeImage sd_setImageWithURL:[NSURL URLWithString:self.mPopImg] placeholderImage:[UIImage imageNamed:[self getCurrentLaunchImageNameForOrientation:UIInterfaceOrientationPortrait]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        if (error) {
                            if (self.delegate && [self.delegate respondsToSelector:@selector(startPage_Fail:)]) {
                                [self.delegate startPage_Fail:self];
                            }
                        }else{
                            self.placeImage.contentMode =  UIViewContentModeScaleAspectFill;
                            self.placeImage.image = image;
                            if (self.delegate ) {
                                [self.delegate startPage_Show];
                            }
                            NSLog(@"1234:%@",[NSDate date]);
                           [self performSelector:@selector(removeLun) withObject:nil afterDelay:5.0];
                        }
                    }];
                }else{
                    if (self.delegate && [self.delegate respondsToSelector:@selector(startPage_Fail:)]) {
                        [self.delegate startPage_Fail:self];
                    }
                }
            }else{
                if (self.delegate && [self.delegate respondsToSelector:@selector(startPage_Fail:)]) {
                    [self.delegate startPage_Fail:self];
                }
            }
        }else{
            if (self.delegate && [self.delegate respondsToSelector:@selector(startPage_Fail:)]) {
                [self.delegate startPage_Fail:self];
            }
        }
    } Failure:^(id _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
    
}

-(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(startPage_Fail:)]) {
        [self.delegate startPage_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 startPage_ClickWithModel:self eventParam:self.mEventParam eventFlag:self.mEventFlag];
    }
}

-(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
