//
//  JPDiscoverCellVideoModule.m
//  JPDiscover
//
//  Created by Jasper on 16/3/25.
//  Copyright 漏 2016骞?Jasper. All rights reserved.
//

#import "JPDiscoverCellVideoModule.h"

@interface JPDiscoverCellVideoModule() {
    UIImageView *thumImage;
    //鍙傛暟
    CGFloat width;
    UILabel *playIcon;
    JPDiscoverVideoModel *dataSource;
}

@end

@implementation JPDiscoverCellVideoModule

#pragma mark init

- (id)initWithWidth:(CGFloat)widthTemp {
    self = [super init];
    if (self) {
        width = widthTemp;
        thumImage = [[UIImageView alloc] init];
        thumImage.contentMode = UIViewContentModeScaleAspectFill;
        thumImage.clipsToBounds = YES;
        thumImage.layer.shouldRasterize = YES;
        thumImage.layer.rasterizationScale = [UIScreen mainScreen].scale;
        thumImage.userInteractionEnabled = YES;
        [self addSubview:thumImage];
        
        playIcon = [[UILabel alloc] init];
        playIcon.backgroundColor = [UIColor clearColor];
        playIcon.userInteractionEnabled = YES;
        playIcon.font = [UIFont fontWithName:@"icomoon" size:35.0f];
        playIcon.textAlignment = NSTextAlignmentCenter;
        playIcon.textColor = [UIColor whiteColor];
        playIcon.text = @"\U0000E650";
        [thumImage addSubview:playIcon];
        
        UITapGestureRecognizer *tapPlay = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPlay)];
        [thumImage addGestureRecognizer:tapPlay];
    }
    return self;
}

#pragma mark setter

- (void)setDataWithModel:(JPDiscoverVideoModel *)model {
    dataSource = model;
    [self refreshViewData];
}

#pragma mark getter

- (CGFloat)height {
    CGFloat showWidth = width - 24;//宸﹀彸闂磋窛鍚?2
    CGFloat showHeight = dataSource.height * showWidth / dataSource.width;
    if (showHeight > showWidth) {
        showHeight = showWidth;
    }
    if(isnan(showWidth)) {
        showWidth=0.0f;
    }
    if (isinf(showWidth)) {
        showWidth=0.0f;
    }
    if(isnan(showHeight)) {
        showHeight=0.0f;
    }
    if (isinf(showHeight)) {
        showHeight=0.0f;
    }
    return showHeight;
}

+ (CGFloat)countHeightWithModel:(JPDiscoverModel *)model width:(CGFloat)width {
    CGFloat showWidth = width - 24;//宸﹀彸闂磋窛鍚?2
    CGFloat showHeight = model.videoModel.height * showWidth / model.videoModel.width;
    if (showHeight > showWidth) {
        showHeight = showWidth;
    }
    if(isnan(showWidth)) {
        showWidth=0.0f;
    }
    if (isinf(showWidth)) {
        showWidth=0.0f;
    }
    if(isnan(showHeight)) {
        showHeight=0.0f;
    }
    if (isinf(showHeight)) {
        showHeight=0.0f;
    }
    return showHeight;
}

#pragma mark 浜嬩欢鏂规硶

- (void)tapPlay {
    self.playVideoBlock();
}

#pragma mark 鏍峰紡鏂规硶

- (void)putImageView {
    CGFloat showWidth = width - 24;//宸﹀彸闂磋窛鍚?2
    CGFloat showHeight = dataSource.height * showWidth / dataSource.width;
    if (showHeight > showWidth) {
        showHeight = showWidth;
        showWidth = dataSource.width * showWidth / dataSource.height;
    }
    if(isnan(showWidth)) {
        showWidth=0.0f;
    }
    if (isinf(showWidth)) {
        showWidth=0.0f;
    }
    if(isnan(showHeight)) {
        showHeight=0.0f;
    }
    if (isinf(showHeight)) {
        showHeight=0.0f;
    }
    thumImage.frame = CGRectMake(12, 0, showWidth, showHeight);
    playIcon.frame = CGRectMake((CGRectGetWidth(thumImage.frame) - 40) / 2, (CGRectGetHeight(thumImage.frame) - 40) / 2, 40, 40);
}

- (void)refreshViewData {
    if (dataSource) {
        self.hidden = NO;
    }else {
        self.hidden = YES;
    }
    //璁剧疆缂╃暐鍥?    self.loadImageBlock(thumImage, dataSource.thumImageUrl, load_default_image);
    [self putImageView];
}

@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com