//
//  InspectUpLoadFootView.m
//  redstar
//
//  Created by admin on 15/11/25.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "InspectUpLoadFootView.h"
#import <UIImageView+WebCache.h>
#import "UIImage+Fit.h"


@implementation InspectUpLoadFootView

- (instancetype)initWithAnswerAttachments:(NSArray *)answerAttachments referenceAttachments:(NSArray *)referenceAttachments remark:(NSString *)remark
{
    self = [super init];
    if (self) {
        self.backgroundColor = kSectionBackGroundColor;
        CGFloat imageMaginX = 20;
        CGFloat imageMaginY = 10;
        CGFloat imageW = (kScreenWidth - imageMaginX * 3) / 2;
        CGFloat imageH = 100;
        
        int answerCount = (int)answerAttachments.count;
        int answerRow = 0;
        if (answerCount % 2 == 0) {
            answerRow = answerCount / 2;
        } else {
            answerRow = (answerCount + 1) / 2;
        }
        
        int referenceCount = (int)referenceAttachments.count;
        int referenceRow = 0;
        if (referenceCount % 2 == 0) {
            referenceRow = referenceCount / 2;
        } else {
            referenceRow = (referenceCount + 1) / 2;
        }
        
        self.height = imageMaginY + (25 * 2) + answerRow * (imageH + imageMaginY) + imageMaginY + referenceRow * (imageH + imageMaginY) + imageMaginY + 100 + 60;
        
        self.titleLabel.text = @"现场照片";

        // 现场照片
        for (int i = 0; i < answerAttachments.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.tag = 1113 + i;
            imageView.userInteractionEnabled = YES;
            NSDictionary *dict = (NSDictionary *)answerAttachments[i];
            NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kRedStarURL ,dict[@"fileUrl"]]];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            [imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default_pic"]];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageCilck:)];
            [imageView addGestureRecognizer:tap];
            int j = 0;
            if ((i + 1) % 2 == 0) {
                j = (i + 1) / 2;
                imageView.frame = CGRectMake(imageMaginX + (imageW + imageMaginX), 35 + imageMaginY + (j-1) * (imageH + imageMaginY), imageW, imageH);
            } else {
                j = (i + 2) / 2;
                imageView.frame = CGRectMake(imageMaginX, 35 + imageMaginY + (j-1) * (imageH + imageMaginY), imageW, imageH);
            }
            [self addSubview:imageView];
        }
        
        UILabel *referenceLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageMaginX, 40 + answerRow * imageH + (answerRow + 1) * imageMaginY, kScreenWidth - imageMaginX * 2, 25)];
        referenceLabel.font = [UIFont systemFontOfSize:15.0];
        referenceLabel.textColor = kLightGray;
        referenceLabel.text = @"参考照片";
        [self addSubview:referenceLabel];
        
        
        
        // 参考照片
        for (int i = 0; i < referenceAttachments.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.userInteractionEnabled = YES;
            NSDictionary *dict = (NSDictionary *)referenceAttachments[i];
            NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kRedStarURL ,dict[@"fileUrl"]]];
        
            [imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default_pic"]];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageCilck:)];
            [imageView addGestureRecognizer:tap];

            int j = 0;
            if ((i + 1) % 2 == 0) {
                j = (i + 1) / 2;
                imageView.frame = CGRectMake(imageMaginX + (imageW + imageMaginX), 65 + answerRow * imageH + (answerRow + 1) * imageMaginY + imageMaginY + (j-1) * (imageH + imageMaginY), imageW, imageH);
            } else {
                j = (i + 2) / 2;
                imageView.frame = CGRectMake(imageMaginX, 65 + answerRow * imageH + (answerRow + 1) * imageMaginY + imageMaginY + (j-1) * (imageH + imageMaginY), imageW, imageH);
            }
            [self addSubview:imageView];
        }
        
        // 备注
        UILabel *remarkLanel = [[UILabel alloc] initWithFrame:CGRectMake(imageMaginX, 65 + answerRow * imageH + (answerRow + 1) * imageMaginY + referenceRow * imageH + (referenceRow + 1) * imageMaginY, kScreenWidth - imageMaginX * 2, 25)];
        remarkLanel.font = [UIFont systemFontOfSize:15.0];
        remarkLanel.textColor = kLightGray;
        remarkLanel.text = @"备注";
        [self addSubview:remarkLanel];
        
        // 背景ImageView
        UIImageView *bgImageView = [[UIImageView alloc] init];
        bgImageView.translatesAutoresizingMaskIntoConstraints = NO;
        UIImage *image = [UIImage imageNamed:@"textbox"];
        UIImage *resizingName = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 24, 24) resizingMode:UIImageResizingModeStretch];
        bgImageView.image = resizingName;
        [self addSubview:bgImageView];
        
        NSLayoutConstraint *overDateTop = [NSLayoutConstraint constraintWithItem:bgImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:remarkLanel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self addConstraint:overDateTop];
        
        NSLayoutConstraint *overDateLeft = [NSLayoutConstraint constraintWithItem:bgImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        [self addConstraint:overDateLeft];
        
        NSLayoutConstraint *overDateRight = [NSLayoutConstraint constraintWithItem:bgImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        [self addConstraint:overDateRight];
        
        
        NSLayoutConstraint *overDateWidth = [NSLayoutConstraint constraintWithItem:bgImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:70];
        [self addConstraint:overDateWidth];
        
        // textView
        UITextView *textView = [[UITextView alloc] init];
        if (remark == nil || remark == NULL || [remark isEqual:[NSNull null]] || remark == Nil || [remark isEqualToString:@"(null)"]) {
            textView.text = [NSString stringWithFormat:@""];
        } else {
            textView.text = [NSString stringWithFormat:@"%@", remark];
        }

        
        textView.backgroundColor = [UIColor clearColor];
        textView.translatesAutoresizingMaskIntoConstraints = NO;
        [bgImageView addSubview:textView];
        
        NSLayoutConstraint *textViewTop = [NSLayoutConstraint constraintWithItem:textView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:bgImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:1];
        [bgImageView addConstraint:textViewTop];
        
        NSLayoutConstraint *textViewLeft = [NSLayoutConstraint constraintWithItem:textView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:bgImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:1];
        [bgImageView addConstraint:textViewLeft];
        
        NSLayoutConstraint *textViewRight = [NSLayoutConstraint constraintWithItem:textView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:bgImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-1];
        [bgImageView addConstraint:textViewRight];
        
        
        NSLayoutConstraint *textViewWidth = [NSLayoutConstraint constraintWithItem:textView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bgImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-1];
        [bgImageView addConstraint:textViewWidth];
        
        // 返回上一页
        UIButton *returnButton = [[UIButton alloc] init];
        returnButton.translatesAutoresizingMaskIntoConstraints = NO;
        returnButton.backgroundColor = kNavigationBarColor;
        returnButton.layer.cornerRadius = 4;
        [returnButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [returnButton setTitle:@"返回上一页面" forState:UIControlStateNormal];
        returnButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [returnButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:returnButton];

        NSLayoutConstraint *returnButtonTop = [NSLayoutConstraint constraintWithItem:returnButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:bgImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
        [self addConstraint:returnButtonTop];
        
        NSLayoutConstraint *returnButtonLeft = [NSLayoutConstraint constraintWithItem:returnButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        [self addConstraint:returnButtonLeft];
        
        NSLayoutConstraint *returnButtonRight = [NSLayoutConstraint constraintWithItem:returnButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        [self addConstraint:returnButtonRight];
        
        NSLayoutConstraint *returnButtonHeight = [NSLayoutConstraint constraintWithItem:returnButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40];
        [self addConstraint:returnButtonHeight];
    }
    return self;
}

- (void)returnClick:(UIButton *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(returnParentsViewController)]) {
        [_delegate returnParentsViewController];
    }
}

- (void)imageCilck:(UITapGestureRecognizer *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(showPicture:)]) {
        [_delegate showPicture:sender];
    }
}

- (UIImage *)image:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}

#pragma - lazy loading
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLabel.font = [UIFont systemFontOfSize:15.0];
        _titleLabel.textColor = kLightGray;
        [self addSubview:_titleLabel];
        
        NSLayoutConstraint *overDateTop = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
        [self addConstraint:overDateTop];
        
        NSLayoutConstraint *overDateLeft = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        [self addConstraint:overDateLeft];
        
        NSLayoutConstraint *overDateRight = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        [self addConstraint:overDateRight];
        
        
        NSLayoutConstraint *overDateWidth = [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25];
        [self addConstraint:overDateWidth];
    }
    return _titleLabel;
}


@end
