//
//  LWImageBrowserModel.m
//  LWAsyncLayerDemo
//
//  Created by 鍒樺井 on 16/2/17.
//  Copyright 漏 2016骞?Warm+. All rights reserved.
//

#import "LWImageBrowserModel.h"
#import "SDWebImageManager.h"
#import "LWDefine.h"


#define kImageBrowserWidth ([UIScreen mainScreen].bounds.size.width + 10.0f)
#define kImageBrowserHeight [UIScreen mainScreen].bounds.size.height



@interface LWImageBrowserModel ()

/**
 *  璁$畻鍚庣殑浣嶇疆
 */
@property (nonatomic,assign,readwrite) CGRect destinationFrame;

/**
 *  鏄惁宸茬粡涓嬭浇
 */
@property (nonatomic,assign,readwrite) BOOL isDownload;


@end

@implementation LWImageBrowserModel

- (id)initWithplaceholder:(UIImage *)placeholder
             thumbnailURL:(NSURL *)thumbnailURL
                    HDURL:(NSURL *)HDURL
       imageViewSuperView:(UIView *)superView
      positionAtSuperView:(CGRect)positionAtSuperView
                    index:(NSInteger)index {
    self = [super init];
    if (self) {
        self.placeholder = placeholder;
        self.thumbnailURL = thumbnailURL;
        self.HDURL = HDURL;
        self.index = index;
        self.title = @"";
        self.contentDescription = @"";
        if (superView != nil) {
            UIWindow* window = [UIApplication sharedApplication].keyWindow;
            CGRect originRect = [superView convertRect:positionAtSuperView toView:window];
            self.originPosition = originRect;
        }
        else {
            self.originPosition = CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0, 0);
        }
    }
    return self;
}


- (id)initWithLocalImage:(UIImage *)localImage
      imageViewSuperView:(UIView *)superView
     positionAtSuperView:(CGRect)positionAtSuperView
                   index:(NSInteger)index {
    self = [super init];
    if (self) {
        self.placeholder = localImage;
        self.index = index;
        self.title = @"";
        self.contentDescription = @"";
        if (superView != nil) {
            UIWindow* window = [UIApplication sharedApplication].keyWindow;
            CGRect originRect = [superView convertRect:positionAtSuperView toView:window];
            self.originPosition = originRect;
        }
        else {
            self.originPosition = CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0, 0);
        }
    }
    return self;
}

/**
 *  璁剧疆鐣ョ缉鍥綰RL
 *
 */
- (void)setThumbnailURL:(NSURL *)thumbnailURL {
    if (_thumbnailURL != thumbnailURL) {
        _thumbnailURL = thumbnailURL;
    }
    if (_thumbnailURL == nil) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    SDWebImageManager* manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:self.thumbnailURL
                          options:0
                         progress:nil
                        completed:^(UIImage *image, NSError *error,
                                    SDImageCacheType cacheType,
                                    BOOL finished,
                                    NSURL *imageURL) {
                            if (finished) {
                                weakSelf.thumbnailImage = image;
                                weakSelf.destinationFrame = [weakSelf calculateDestinationFrameWithSize:weakSelf.thumbnailImage.size
                                                                                                  index:weakSelf.index];
                            }
                        }];
}

/**
 *
 *   璁剧疆鐣ョ缉鍥剧殑鏃跺€欒绠楅€傞厤灞忓箷鐨勫ぇ灏? */

- (CGRect)calculateDestinationFrameWithSize:(CGSize)size
                                      index:(NSInteger)index {
    CGRect rect = CGRectMake(kImageBrowserWidth * index,
                             (kImageBrowserHeight - size.height * [UIScreen mainScreen].bounds.size.width / size.width)/2,
                             [UIScreen mainScreen].bounds.size.width,
                             size.height * [UIScreen mainScreen].bounds.size.width / size.width);
    return rect;
}

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