//
//  KKInsuranceContainerController.m
//  Open
//
//  Created by 雷俊博 on 2021/4/28.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKInsuranceContainerController.h"
#import "KKInsuranceViewController.h"

@interface KKInsuranceContainerController ()<UIScrollViewDelegate>
{
    UIButton* lastSelectBtn;
}

@property (nonatomic, strong)UIScrollView* maxScrollView;

@property (nonatomic, strong)UIView *kTabBarView;

@property (nonatomic, strong)UIButton *leftBtn;

@property (nonatomic, strong)UIButton *rightBtn;

@property (nonatomic, strong)NSMutableArray *vcArray;

@end

@implementation KKInsuranceContainerController

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.vcArray = [NSMutableArray array];
    
    [self.view addSubview:self.maxScrollView];
    [self.view addSubview:self.kTabBarView];
    
    [self addSingleSidesShadowToView:self.kTabBarView withColor:[UIColor blackColor]];
    
    
    for (int i = 0; i < 2; i++) {
        
        KKInsuranceModel *model = self.dataArray[i];
        KKInsuranceViewController *vc = [KKInsuranceViewController new];
        [self addChildViewController:vc];
        vc.title = model.title;
        vc.view.backgroundColor = [UIColor whiteColor];
        vc.url = model.path;
        
        vc.view.frame = CGRectMake(i * ScreenWidth, 0, ScreenWidth, self.maxScrollView.bounds.size.height);
        
       
        //[vc beginAppearanceTransition:YES animated:NO];
        [self.maxScrollView addSubview:vc.view];
        
//        [vc endAppearanceTransition];
//        [vc didMoveToParentViewController:self];
        [vc didMoveToParentViewController:self];
        
        [self.vcArray addObject:vc];
        
    }
    
    
    if (@available(iOS 11.0, *)) {
           self.maxScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
       } else {
           self.automaticallyAdjustsScrollViewInsets = NO;
       }
    
    [self configNav];
}

- (void)configNav
{
    UIButton * button_2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button_2.frame = CGRectMake(-30, 0, 60, 44);
    button_2.titleLabel.textAlignment =NSTextAlignmentLeft;
    button_2.titleLabel.font = [UIFont systemFontOfSize:17];
    [button_2 setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [button_2 setTitle:@"返回" forState:UIControlStateNormal];
    [button_2 layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:2];
    [button_2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button_2 addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * backButtonItem_2 = [[UIBarButtonItem alloc] initWithCustomView:button_2];
    
    self.navigationItem.leftBarButtonItem = backButtonItem_2;
}



#pragma mark - 保险统计
/**
 * 请求类型
 * 100101保险_banner入口
 * 100102保险_菜单入口
 * 100103保险_任务入口
 * 100104保险_重疾险
 * 100105保险_综合险
 */
- (void)makeInsuranceStatisticsRequestWithType:(NSString *)type
{
    NSString *url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYinShuConfig],KKInsuranceStatisticsRequest];
    
    NSString *province = [Tools isBlankString:[UserDefault objectForKey:@"locationProvince"]] ? @"" : [UserDefault objectForKey:@"locationProvince"];
    NSString *city = [Tools isBlankString:[UserDefault objectForKey:@"locationCity"]] ? @"" : [UserDefault objectForKey:@"locationCity"];
    NSString *county = [Tools isBlankString:[UserDefault objectForKey:@"locationCounty"]] ? @"" : [UserDefault objectForKey:@"locationCounty"];
    
    NSMutableDictionary *params = [NetworkPublicParams getBCPublicParams];
    
    
    [params setObject:[EnercryTool encryptUseDES:[getPeopleID PeopleIphoneNmber] key:DESKEY] forKey:@"tel"];
    [params setObject:@([getPeopleID PeopleID]) forKey:@"uid"];
    [params setObject:province forKey:@"province"];
    [params setObject:city forKey:@"city"];
    [params setObject:county forKey:@"county"];
    [params setObject:@"kksh" forKey:@"fromApp"];
    [params setObject:type forKey:@"type"];
   
   
    NSLog(@"params:%@",params);
    
    
    [NetworkRequest Post15:url Params:params Success:^(id _Nonnull objc) {
    
        NSLog(@"objc:%@",objc);
        

    } Failure:^(NSError* error) {
       
        NSLog(@"error:%@",error);
        
       
        
    }];
    
}



#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollviewW =  scrollView.frame.size.width;
    
    CGFloat x = scrollView.contentOffset.x;
    
    int page = (x + scrollviewW / 2) /  scrollviewW;
    
    if (page == 0) {
        [self btnClick:self.leftBtn];
    }else if (page == 1){
        [self btnClick:self.rightBtn];
    }
    
}


#pragma mark - event response
- (void)backClick:(UIButton *)sender
{
    NSInteger index = lastSelectBtn.tag - 200;
    KKInsuranceViewController *vc = self.vcArray[index];
    
    
    NSLog(@"%lu",vc.webView.backForwardList.backList.count);
    
    if (vc.webView.backForwardList.backList.count > 0) {
        WKNavigation *navigation = [vc.webView goBack];
        NSInteger offset = 1;
        while (!navigation)
        {
            offset++;
            if (vc.webView.backForwardList.backList.count >= offset)
            {
                WKBackForwardListItem *item1 = [vc.webView.backForwardList itemAtIndex:-offset];
                navigation = [vc.webView goToBackForwardListItem:item1];
            }
            else
            {
                break;
            }
        }
        NSLog(@"WKNavigation - %@",navigation);
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }

    
}
- (void)btnClick:(UIButton *)sender {
    if (sender == lastSelectBtn) {
        return;
    }
    lastSelectBtn.selected = NO;
    sender.selected = YES;
    lastSelectBtn = sender;
    
    [self.maxScrollView setContentOffset:CGPointMake((lastSelectBtn.tag - 200) * ScreenWidth, self.maxScrollView.contentOffset.y) animated:NO];
    
    
    KKInsuranceModel *model = self.dataArray[lastSelectBtn.tag - 200];
    
    self.title = model.title;
    
    if ([model.pageName isEqualToString:@"重疾险"]) {
        [self makeInsuranceStatisticsRequestWithType:@"100104"];
    }else if ([model.pageName isEqualToString:@"综合险"]){
        [self makeInsuranceStatisticsRequestWithType:@"100105"];
    }
   
}

#pragma mark - 在顶部添加阴影
//在顶部添加阴影
-(void)addSingleSidesShadowToView:(UIView *)theView withColor:(UIColor*)theColor{
    //阴影颜色
    theView.layer.shadowColor = theColor.CGColor;
    //阴影偏移
    theView.layer.shadowOffset = CGSizeMake(0, 0 );
    //阴影透明度，默认0
    theView.layer.shadowOpacity = 0.3;
    //阴影半径，默认3
    theView.layer.shadowRadius = 8;
    //单边阴影
    CGFloat shadowPathWidth = theView.layer.shadowRadius;
    CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2, theView.bounds.size.width, shadowPathWidth);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
    theView.layer.shadowPath = path.CGPath;
}

#pragma mark - getters and setters

- (UIScrollView *)maxScrollView
{
    if (!_maxScrollView) {
        _maxScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight  - cl_kTabBarHeight)];
        _maxScrollView.showsHorizontalScrollIndicator = NO;
        //_maxScrollView.contentOffset = CGPointZero;
        _maxScrollView.contentSize = CGSizeMake(ScreenWidth * 2, ScreenHeight - cl_kTabBarHeight);
        _maxScrollView.scrollsToTop = NO;
        _maxScrollView.pagingEnabled = YES;
        _maxScrollView.delegate = self;
        _maxScrollView.bounces = NO;
        _maxScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    }
    
    return _maxScrollView;
}


#pragma mark - getters and setters
- (UIView *)kTabBarView {
    if (!_kTabBarView) {
        _kTabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - cl_kTabBarHeight, ScreenWidth, cl_kTabBarHeight)];
        _kTabBarView.backgroundColor = [UIColor whiteColor];
        
        KKInsuranceModel* leftModel = self.dataArray[0];
        KKInsuranceModel* rightModel = self.dataArray[1];
        
        UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.frame = CGRectMake(0, 0, ScreenWidth/2, 49);
        [leftBtn setTitle:leftModel.pageName forState:UIControlStateNormal];
        [leftBtn setTitleColor:K_MainColor forState:UIControlStateSelected];
        [leftBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
        leftBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        leftBtn.tag = 200;
        [leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_kTabBarView addSubview:leftBtn];
        self.leftBtn = leftBtn;
        
        UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(ScreenWidth/2, 0, ScreenWidth/2, 49);
        [rightBtn setTitle:rightModel.pageName forState:UIControlStateNormal];
        [rightBtn setTitleColor:K_MainColor forState:UIControlStateSelected];
        [rightBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        rightBtn.tag = 201;
        [rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_kTabBarView addSubview:rightBtn];
        self.rightBtn = rightBtn;
        
        
        leftBtn.selected = YES;
        lastSelectBtn = leftBtn;
    }
    return _kTabBarView;
}

/*
#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
