//
//  DeathProveViewController.m
//  Open
//
//  Created by 雷俊博 on 2019/7/26.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "DeathProveViewController.h"

#import "Constants.h"

#import <Masonry.h>

@interface DeathProveViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIView *topBackView;
@property (nonatomic, strong)UILabel *lb_locationTitle;

@property (nonatomic, strong)UIImageView *img_location;

@property (nonatomic, strong)UILabel *lb_address;

@property (nonatomic, strong)UIView *middleView;

@property (nonatomic, strong)UIImageView *img_add;

@property (nonatomic, strong)UIButton *btn_addPhoto;

@property (nonatomic, strong)UILabel *lb_attention;

@property (nonatomic, strong)UIButton *btn_commit;


@property (nonatomic, strong)UIImagePickerController *imagePicker;
@property (nonatomic, strong)NSMutableArray *imagesArray;

@end

@implementation DeathProveViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout=UIRectEdgeBottom;
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.btn_commit];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(0, 2, 40, 40);
    
    [btn setTitle:@"重拍" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 200;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    
    negativeSpacer.width = -40;
    
    UIBarButtonItem *negativeSpacer2 = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    
    negativeSpacer2.width = 40;
    
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, item,negativeSpacer2, nil];
    
}
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    [self.lb_locationTitle  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10 * WIDTHRADIU);
        make.left.offset(20 * WIDTHRADIU);
        make.height.offset(18 * WIDTHRADIU);
    }];
    [self.img_location mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lb_locationTitle);
        make.centerY.equalTo(self.lb_address);
    }];
    
    [self.lb_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_locationTitle.mas_bottom).offset(10 * WIDTHRADIU);
        make.left.equalTo(self.img_location.mas_right).offset(5 * WIDTHRADIU);
        make.right.equalTo(self.topBackView).offset(-40 * WIDTHRADIU);
    }];
    
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_address.mas_bottom).offset(18 * WIDTHRADIU);
        make.centerX.equalTo(self.topBackView);
        make.width.offset(ScreenWidth - 40 * WIDTHRADIU);
        make.height.offset(ScreenWidth - 40 * WIDTHRADIU);
    }];
    
    [self.img_add mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.middleView);
    }];
    [self.btn_addPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
    
    [self.lb_attention mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.middleView.mas_bottom).offset(15 * WIDTHRADIU);
        make.left.equalTo(self.middleView);
        make.right.equalTo(self.middleView);
    }];
    
    [self.btn_commit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_bottom).offset(0);
        make.left.right.offset(0);
        make.height.offset(45 * WIDTHRADIU);
    }];
}

#pragma mark - event response
-(void)btnClick:(UIButton *)sender
{
    
    if (sender.tag == 200 || sender.tag == 201) {
         [self getPhoto];
    }
   
}

#pragma mark - 获取照片
- (void)getPhoto
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择获取图片操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *photos = [UIAlertAction actionWithTitle:@"从相册中" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        self.imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
//        [self presentViewController:self.imagePicker animated:YES completion:nil];
//        
//    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *makePhotos = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
        [self presentViewController: self.imagePicker animated:YES completion:nil];
        
    }];
    //[alert addAction:photos];
    [alert addAction:makePhotos];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController: (UIImagePickerController *)picker
didFinishPickingMediaWithInfo: (NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (self.imagesArray.count == 0) {
         [self.imagesArray addObject:image];
        
    }else if (self.imagesArray.count == 1){
        [self.imagesArray replaceObjectAtIndex:0 withObject:image];
    }
   [self.btn_addPhoto setBackgroundImage:image forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 用户选择取消
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - getters and setters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - cl_kNavigationBarHeight - 45 * WIDTHRADIU)];
        _scrollView.contentSize = CGSizeMake(ScreenWidth, ScreenHeight+20* WIDTHRADIU);
        _scrollView.showsHorizontalScrollIndicator = NO;
        [_scrollView addSubview:self.topBackView];
        [_scrollView addSubview:self.lb_locationTitle];
        [_scrollView addSubview:self.img_location];
        [_scrollView addSubview:self.lb_address];
        [_scrollView addSubview:self.middleView];
        [_scrollView addSubview:self.lb_attention];

        
    }
    return _scrollView;
}
- (UIView *)topBackView
{
    if (!_topBackView) {
        _topBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 254 * WIDTHRADIU)];
        _topBackView.backgroundColor = K_MainColor;
    }
    return _topBackView;
}

- (UILabel *)lb_locationTitle
{
    if (!_lb_locationTitle) {
        _lb_locationTitle = [[UILabel alloc]init];
        _lb_locationTitle.text = @"当前位置";
        _lb_locationTitle.textColor = [UIColor whiteColor];
        _lb_locationTitle.font = [UIFont boldSystemFontOfSize:18 * WIDTHRADIU];
    }
    return _lb_locationTitle;
}

- (UIImageView *)img_location
{
    if (!_img_location) {
        _img_location = [[UIImageView alloc]init];
        _img_location.image = [UIImage imageNamed:@"my_Socialsecuritytermination_deathcertificate_Positioning_normal"];
    }
    return _img_location;
}

- (UILabel *)lb_address
{
    if (!_lb_address) {
        _lb_address = [[UILabel alloc]init];
        _lb_address.text = @"上海市浦东新区碧波路439号213134328758973489927348957234897893768973";
        _lb_address.textColor = [UIColor whiteColor];
        _lb_address.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
        _lb_address.numberOfLines = 0;
    }
    return _lb_address;
}

- (UIView *)middleView
{
    if (!_middleView) {
        _middleView = [[UIView alloc]init];
        _middleView.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
        [_middleView addSubview:self.img_add];
        [_middleView addSubview:self.btn_addPhoto];
    }
    return _middleView;
}

- (UIImageView *)img_add
{
    if (!_img_add) {
        _img_add = [[UIImageView alloc]init];
        _img_add.image = [UIImage imageNamed:@"my_Socialsecuritytermination_deathcertificate_uploadimage_normal"];
    }
    return _img_add;
}

- (UIButton *)btn_addPhoto
{
    if (!_btn_addPhoto) {
        _btn_addPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_addPhoto addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _btn_addPhoto.tag = 201;
    }
    return _btn_addPhoto;
}

- (UILabel *)lb_attention
{
    if (!_lb_attention) {
        _lb_attention = [[UILabel alloc]init];
        _lb_attention.numberOfLines = 0;
        NSString * str1 = @"注意:\n";
        NSString *str2 = @"1、请提交有效的死亡证明或村委会证明（需签章）材料；\n2、上传虚假材料将依法追究责任人；\n3、上传图片时注意清晰度，确保重要信息的可读性。";
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineSpacing = 12 * WIDTHRADIU;
        NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080], NSFontAttributeName:[UIFont systemFontOfSize:15 * WIDTHRADIU], NSParagraphStyleAttributeName:paragraphStyle};
        NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle2.lineSpacing = 11 * WIDTHRADIU;
        NSDictionary *attributes2 = @{NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080], NSFontAttributeName:[UIFont systemFontOfSize:14 * WIDTHRADIU], NSParagraphStyleAttributeName:paragraphStyle2};
        
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",str1,str2]];
        
       
        [attStr addAttributes:attributes range:NSMakeRange(0, str1.length)];
        [attStr addAttributes:attributes2 range:NSMakeRange(str1.length, str2.length)];
        _lb_attention.attributedText = attStr;
    }
    return _lb_attention;
}

- (UIButton *)btn_commit
{
    if (!_btn_commit) {
        _btn_commit = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_commit setTitle:@"上传死亡证明" forState:UIControlStateNormal];
        [_btn_commit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_commit.titleLabel.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
        _btn_commit.backgroundColor = K_MainColor;
        [_btn_commit addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btn_commit.tag = 202;
    }
    return _btn_commit;
}
- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.delegate = self;
        _imagePicker.allowsEditing = YES;
    }
    
    return _imagePicker;
}

- (NSMutableArray *)imagesArray
{
    if (!_imagesArray) {
        _imagesArray = [[NSMutableArray alloc]initWithCapacity:2];
        
    }
    return _imagesArray;
}
/*
#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
