// Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import Foundation
import UIKit
import FBSDKShareKit

/**
 A photo for sharing.
 */
public struct Photo {
  ///  If the photo is resident in memory, this method supplies the data.
  public var image: UIImage?

  /// The URL to the photo.
  public var url: URL?

  /// Specifies whether the photo represented by the receiver was generated by the user or by the application.
  public var isUserGenerated: Bool

  /**
   The user generated caption for the photo. Note that the 'caption' must come from the user, as pre-filled content
   is forbidden by the Platform Policies (2.3).
   */
  public var caption: String?

  /**
   Conveniece method to Create a `Photo` with an image.

   - parameter image:         The image to create with.
   - parameter userGenerated: Whether or not this image was user generated.
   */
  public init(image: UIImage, userGenerated: Bool) {
    self.image = image
    self.isUserGenerated = userGenerated
  }

  /**
   Conveniece method to Create a `Photo` with an image.

   - parameter url:           The image URL to create with.
   - parameter userGenerated: Whether or not this image was user generated.
   */
  public init(url: URL, userGenerated: Bool) {
    self.url = url
    self.isUserGenerated = userGenerated
  }
}

extension Photo {
  internal var sdkPhotoRepresentation: FBSDKSharePhoto {
    let photo = FBSDKSharePhoto()
    photo.image = image
    photo.imageURL = url
    photo.isUserGenerated = isUserGenerated
    photo.caption = caption

    return photo
  }

  internal init(sdkPhoto: FBSDKSharePhoto) {
    self.image = sdkPhoto.image
    self.url = sdkPhoto.imageURL
    self.isUserGenerated = sdkPhoto.isUserGenerated
    self.caption = sdkPhoto.caption
  }
}

extension Photo: Equatable {
  /**
   Compare to photos for equality.

   - parameter lhs: The first photo to compare.
   - parameter rhs: The second photo to compare.

   - returns: Whether or not the photos are equal.
   */
  public static func == (lhs: Photo, rhs: Photo) -> Bool {
    return lhs.sdkPhotoRepresentation == rhs.sdkPhotoRepresentation
  }
}
