//
//  KKDealIdPhotoVC.m
//  Open
//
//  Created by qxy on 2021/10/13.
//  Copyright © 2021 QXY . All rights reserved.
//

#import "KKDealIdPhotoVC.h"
#import "KKPictureEditVC.h"
#import "KKCarPictureEditVC.h"
#import "IdPhotoBottom.h"
#import "WebVC.h"
@interface KKDealIdPhotoVC ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,IdPhotoBottomDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) UIView *navBar;
@property (nonatomic, assign) UIView *centerGroup;
@property (nonatomic, strong)NSArray* bottomArray;
@end

@implementation KKDealIdPhotoVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithHex:0xF5F5F5];
    self.title = @"AI照相馆";
    [self navConfig];
    [self centerView];
    [self bottomView];
}
-(void)bottomView{

    UILabel* bottomText = [UILabel new];
    bottomText.text=@"| 更多服务";
    bottomText.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    bottomText.textColor=[UIColor colorWithHex:0x4c4c4c];
    [self.view addSubview:bottomText];
    [bottomText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.centerGroup.mas_bottom).offset(10*WIDTHRADIU);
        make.left.offset(10*WIDTHRADIU);
        
    }];
    
    UIView*bottomGroup = [[UIView alloc]init];
    bottomGroup.backgroundColor=[UIColor whiteColor];
    bottomGroup.layer.cornerRadius = 10 * WIDTHRADIU;
    bottomGroup.layer.masksToBounds = YES;
    [self.view addSubview:bottomGroup];
    [bottomGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomText.mas_bottom).offset(15*WIDTHRADIU);
        make.width.offset(SCREEN_WIDTH-20*WIDTHRADIU);
        make.height.offset(100*WIDTHRADIU);
        make.centerX.equalTo(self.view);
    }];
    
    IdPhotoBottom *bottomView=[[IdPhotoBottom alloc]initWithFrame:CGRectMake(0, 10*WIDTHRADIU, ScreenWidth-20*WIDTHRADIU, 80*WIDTHRADIU)];
    bottomView.delegate=self;
    bottomView.reloadData;
    [bottomGroup addSubview:bottomView];
//    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.offset(10*WIDTHRADIU);
//    }];
    
    UIImageView* bottomImage = [UIImageView new];
    bottomImage.image=[UIImage imageNamed:@"id_photo_bottom"];
    bottomImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *bottomImage01 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBottomImage:)];
    [bottomImage addGestureRecognizer:bottomImage01];
    [self.view addSubview:bottomImage];
    [bottomImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomGroup.mas_bottom).offset(10*WIDTHRADIU);
        make.height.offset(90*WIDTHRADIU);
        make.width.offset(SCREEN_WIDTH-20*WIDTHRADIU);
        make.centerX.equalTo(self.view);
    }];
    
    NSString *url = [NSString stringWithFormat:@"%@dynamic/menu/get",[NetworkConfig ipYinShuConfig]];
    NSDictionary *params = @{
        @"configName":@"IdPhotoBottom",
        @"appName":@"kksh",
        @"version":[Tools getVersionCode],
        @"systemType":@"iOS"
    };
    [NetworkRequest Post15:url Params:params Success:^(id _Nonnull objc) {
        NSLog(@"objc:%@",objc);
        if ([objc isKindOfClass:[NSDictionary class]]) {
            NSString *state = [NSString stringWithFormat:@"%@",objc[@"state"]];
            if ([state isEqualToString:@"200"]) {
                if ([objc[@"result"] isKindOfClass:[NSArray class]]) {
                    self.bottomArray = [KKRedPackageClickEventModel mj_objectArrayWithKeyValuesArray:objc[@"result"]];
                    KKRedPackageClickEventModel *model=self.bottomArray[0];
                    [bottomImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
                }
            }
        }
    } Failure:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

-(void)clickBottomImage:(UITapGestureRecognizer *)tap{
    NSLog(@"%@",@"!!!!!!!!!!!!");
    if(self.bottomArray!=nil&&self.bottomArray.count>0){
        KKRedPackageClickEventModel *model= self.bottomArray[0];
        [[clickEvent instance] clickEventRooterVC:self.navigationController model:model];
    }else{
        [[clickEvent instance] clickEventRooterVC:self Content:@"ftHexinV2" menuName:@"ftHexinV2"];
    }
}
- (void)centerView{
    UIImageView* topImage = [UIImageView new];
    topImage.image=[UIImage imageNamed:@"base_id_tu"];
    topImage.contentMode = UIViewContentModeScaleAspectFill;
    //topImage.backgroundColor = [UIColor clearColor];
    [self.view addSubview:topImage];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navBar.mas_bottom);
        make.width.offset(SCREEN_WIDTH);
        make.height.offset(300*WIDTHRADIU);
    }];
    
    UIView*centerGroup = [[UIView alloc]init];
    centerGroup.backgroundColor=[UIColor whiteColor];
    centerGroup.layer.cornerRadius = 10 * WIDTHRADIU;
    centerGroup.layer.masksToBounds = YES;
    [self.view addSubview:centerGroup];
    [centerGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage.mas_bottom).offset(-50*WIDTHRADIU);
        make.width.offset(SCREEN_WIDTH-20*WIDTHRADIU);
        make.height.offset(100*WIDTHRADIU);
        make.centerX.equalTo(self.view);
    }];
    self.centerGroup=centerGroup;
    UIImageView* leftImage = [UIImageView new];
    leftImage.image=[UIImage imageNamed:@"id_photo_left"];
    leftImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *leftImage01 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLeftImage:)];
    [leftImage addGestureRecognizer:leftImage01];
    [centerGroup addSubview:leftImage];
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25*WIDTHRADIU);
        make.left.offset(50*WIDTHRADIU);
        make.width.height.offset(30*WIDTHRADIU);
    }];
    
    UILabel* leftText = [UILabel new];
    leftText.text=@"拍摄证件照";
    leftText.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    leftText.textColor=[UIColor colorWithHex:0x4c4c4c];
    [centerGroup addSubview:leftText];
    [leftText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftImage.mas_bottom).offset(8*WIDTHRADIU);
        make.centerX.equalTo(leftImage);
    }];
    
    UIImageView* rightImage = [UIImageView new];
    rightImage.image=[UIImage imageNamed:@"id_photo_right"];
    rightImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *rightImage01 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRightImage:)];
    [rightImage addGestureRecognizer:rightImage01];
    [centerGroup addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10*WIDTHRADIU);
        make.left.equalTo(leftImage.mas_right).offset(ScreenWidth-190*WIDTHRADIU);
        make.width.height.offset(40*WIDTHRADIU);
    }];
    
    UILabel* rightText = [UILabel new];
    rightText.text=@"拍摄卡通照";
    rightText.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    rightText.textColor=[UIColor colorWithHex:0x4c4c4c];
    [centerGroup addSubview:rightText];
    [rightText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rightImage.mas_bottom).offset(8*WIDTHRADIU);
        make.centerX.equalTo(rightImage);
        
    }];
    
    UIImageView* centerImage = [UIImageView new];
    centerImage.backgroundColor=[UIColor colorWithHex:0x8c8c8c];
    [centerGroup addSubview:centerImage];
    [centerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20*WIDTHRADIU);
        make.centerX.equalTo(centerGroup);
        make.width.offset(0.5);
        make.height.offset(70*WIDTHRADIU);
    }];
    
    
}

- (void)navConfig{
    UIView*navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, cl_kNavigationBarHeight)];
    navView.backgroundColor = [UIColor whiteColor];
    //返回按钮
    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"blackBack"] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor colorWithHex:0x4d4d4d] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(10, cl_kStatusBarHeight, 60, 44);
    backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [backBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    [backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel* lb_title = [UILabel new];
    lb_title.text = @"AI照相馆";
    lb_title.font = [UIFont boldSystemFontOfSize:18];
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    [navView addSubview:backBtn];
    [navView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(cl_kStatusBarHeight);
        make.centerX.equalTo(navView);
        make.height.offset(44);
    }];
    self.navBar=navView;
    [self.view addSubview:navView];
}


#pragma mark - event response
- (void)backClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
//证件照信息
-(void)clickLeftImage:(UITapGestureRecognizer *)tap{
    if ([ToolsObject getAuthorizationStatus] == NO) {
        return;
    }
    self.index = 10;
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"相机拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }];
    [alertVc addAction:takePhotoAction];
    UIAlertAction *imagePickerAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openPhotoLibrary];
    }];
    [alertVc addAction:imagePickerAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:cancelAction];
    [self presentViewController:alertVc animated:YES completion:nil];
}

//卡通照信息
-(void)clickRightImage:(UITapGestureRecognizer *)tap{
    if ([ToolsObject getAuthorizationStatus] == NO) {
        return;
    }
    self.index = 0;
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"相机拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }];
    [alertVc addAction:takePhotoAction];
    UIAlertAction *imagePickerAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openPhotoLibrary];
    }];
    [alertVc addAction:imagePickerAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:cancelAction];
    [self presentViewController:alertVc animated:YES completion:nil];
}


#pragma mark ---相机---
- (void)openCamera{
    NSUInteger soureceType  = UIImagePickerControllerSourceTypeCamera;
   UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
   imagePickerController.delegate = self;
   imagePickerController.sourceType = soureceType;
   //imagePickerController.allowsEditing = YES;
   [self presentViewController:imagePickerController animated:YES completion:nil];
}
#pragma mark ---相册---
- (void)openPhotoLibrary{
    NSUInteger soureceType  = UIImagePickerControllerSourceTypePhotoLibrary;
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = soureceType;
    //imagePickerController.allowsEditing = YES;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
//------------------------------cammer--------------------------------
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        [self dismissViewControllerAnimated:YES completion:^{
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
            if (self.index == 10) {
                KKPictureEditVC *vc = [[KKPictureEditVC alloc]init];
                vc.image = image;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                KKCarPictureEditVC *vc = [[KKCarPictureEditVC alloc]init];
                vc.image = image;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }];
    });
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - ViewDelegate 按钮跳转
- (void)IdPhotoBottomWithItem:(NSInteger)item:(KKRedPackageClickEventModel*)model{
    NSLog(@"111111");
    if(model==nil){
        switch (item) {
            case 0:
                //Morecredit
                [[clickEvent instance] clickEventRooterVC:self Content:@"Morecredit" menuName:@"Morecredit"];
                break;
            case 1:
                [[clickEvent instance] clickEventRooterVC:self Content:@"homeThree2" menuName:@"homeThree2"];
                break;
            case 2:
                //homeThree4
                [[clickEvent instance] clickEventRooterVC:self Content:@"homeThree4" menuName:@"homeThree4"];
                break;
            case 3:
                //拼多多 pinduoduoH5
                [[clickEvent instance] clickEventRooterVC:self Content:@"pinduoduoH5" menuName:@"pinduoduoH5"];
                break;
        }
    }else{
        [[clickEvent instance]clickEventNative:self model:model];
    }
}
/*
#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
