//
//  CDYelpBusiness.swift
//  CDYelpFusionKit
//
//  Created by Christopher de Haan on 11/29/16.
//
//  Copyright © 2016-2021 Christopher de Haan <contact@christopherdehaan.me>
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission 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.
//
#if os(macOS)
    import AppKit
#endif
#if !os(OSX)
    import UIKit
#else
    import Foundation
#endif

@objc @objcMembers public class CDYelpBusiness: NSObject, Decodable {

    public let id: String?
    public let name: String?
    public let imageUrl: URL?
    public let isClosed: Bool?
    public let url: URL?
    public let price: String?
    public let phone: String?
    public let displayPhone: String?
    public let photos: [String]?
    public let hours: [CDYelpHour]?
    public let rating: Double?
    public let reviewCount: Int?
    public let categories: [CDYelpCategory]?
    public let distance: Double?
    public let coordinates: CDYelpCoordinates?
    public let location: CDYelpLocation?
    public let transactions: [String]?
#if os(macOS)
    public var transactionsString: NSString {
        if let strings = transactions {
            let words: [String] = strings.map {
                if $0 == "restaurant_reservation" {
                    return "📆"
                } else if $0 == "delivery" {
                    return "📦"
                } else if $0 == "pickup" {
                    return "🥡"
                }
                return ""
            }
            let list = NSString(string: words.joined(separator: ""))
            return list
        }
        return NSString()
    }
    public var categoriesString: NSString {
        if let categories = categories {
            let words: [String] = categories.compactMap{ return $0.title }
            let list = NSString(string: words.joined(separator: ", "))
            return list
        }
        return NSString()
    }
    public var hoursString: NSAttributedString? {
        if let hours = hours {
            var total: String = ""
            let df: DateFormatter = DateFormatter()
            for hour in hours {
                if let specifics = hour.open {
                    for specific in specifics {
                        if let day = specific.day, let start = specific.start, let end = specific.end {
                            switch day {
                            case 0:
                                total += "Mon "
                            case 1:
                                total += "Tue "
                            case 2:
                                total += "Wed "
                            case 3:
                                total += "Thu "
                            case 4:
                                total += "Fri "
                            case 5:
                                total += "Sat "
                            case 6:
                                total += "Sun "
                            default:
                                break
                            }
                            df.dateFormat = "HHmm"
                            if let sdate = df.date(from: start), let edate = df.date(from: end) {
                                df.dateFormat = "h:mma"
                                let s = df.string(from: sdate)
                                let e = df.string(from: edate)
                                total += "\t\(s)-\(e)\n"
                            }
                        }
                    }
                }
            }
            return NSAttributedString(string: total,
                                      attributes: [NSAttributedString.Key.font: NSFont.systemFont(ofSize: 10)])
        }
        return nil
    }
#endif
    public var multiplePhotos: Bool {
        if let photos = photos {
            return photos.count > 1
        }
        return false
    }

    enum CodingKeys: String, CodingKey {
        case id
        case name
        case imageUrl = "image_url"
        case isClosed = "is_closed"
        case url
        case price
        case phone
        case displayPhone = "display_phone"
        case photos
        case hours
        case rating
        case reviewCount = "review_count"
        case categories
        case distance
        case coordinates
        case location
        case transactions

    }
}
