//
//  PickerPhoto.m
//  ZLAssetsPickerDemo
//
//  Created by 张磊 on 14-11-15.
//  Copyright (c) 2014年 com.zixue101.www. All rights reserved.
//

#import "ZLPhotoPickerBrowserPhoto.h"
#import "ZLCamera.h"

@implementation ZLPhotoPickerBrowserPhoto

- (void)setPhotoObj:(id)photoObj
{
    _photoObj = photoObj;
    
    if ([photoObj isKindOfClass:[ZLPhotoAssets class]]) {
        ZLPhotoAssets *asset = (ZLPhotoAssets *)photoObj;
        self.asset = asset;
    } else if ([photoObj isKindOfClass:[NSURL class]]){
        self.photoURL = photoObj;
    } else if ([photoObj isKindOfClass:[UIImage class]]){
        self.photoImage = photoObj;
    } else if ([photoObj isKindOfClass:[NSString class]]){
        self.photoURL = [NSURL URLWithString:photoObj];
    } else if ([photoObj isKindOfClass:[ZLCamera class]]){
        self.photoImage = (UIImage *)[photoObj fullScreenImage];
    } else {
        NSAssert(true == true, @"您传入的类型有问题");
    }
}

- (UIImage *)photoImage
{
    if (!_photoImage) {
        if (self.asset) {
            _photoImage = [self.asset originImage];
        } else if ([_toView isKindOfClass:[UIImageView class]]) {
            _photoImage = ((UIImageView *)_toView).image;
        } else if ([_toView isKindOfClass:[UIButton class]]) {
            _photoImage = [((UIButton *)_toView) imageForState:UIControlStateNormal];
        }
    }
    return _photoImage;
}

- (UIImage *)thumbImage
{
    if (!_thumbImage) {
        if (self.asset) {
            _thumbImage = [self.asset thumbImage];
        } else if (_photoImage) {
            _thumbImage = _photoImage;
        } else if ([_toView isKindOfClass:[UIImageView class]]) {
            _thumbImage = ((UIImageView *)_toView).image;
        } else if ([_toView isKindOfClass:[UIButton class]]) {
            _thumbImage = [((UIButton *)_toView) imageForState:UIControlStateNormal];
        }
    }
    return _thumbImage;
}

#pragma mark - 传入一个图片对象，可以是URL/UIImage/NSString，返回一个实例
+ (instancetype)photoAnyImageObjWith:(id)imageObj
{
    ZLPhotoPickerBrowserPhoto *photo = [[self alloc] init];
    [photo setPhotoObj:imageObj];
    return photo;
}

@end
