//
//  PQLoadingHUB.swift
//  PQSpeed
//
//  Created by SanW on 2020/6/5.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit

public class PQLoadingHUBView: UIView {
    // gif每一帧图
    public var gifImages: [UIImage]?
    // gif播放时长
    public var duration: Double?

    public lazy var loadingImage: UIImageView = {
        let loadingImage = UIImageView()
        loadingImage.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        return loadingImage
    }()

    override public init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(loadingImage)
        isUserInteractionEnabled = false
        let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_loading", ofType: ".gif")!))
        if data != nil {
            PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { [weak self] _, images, duration in
                self?.gifImages = images
                self?.duration = duration
            }
        }
    }

    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
        // 334 * 307
        let imageW: CGFloat = 67
        let imageH: CGFloat = 62
        loadingImage.frame = CGRect(x: (frame.width - imageW) / 2, y: (frame.height - imageW) / 2, width: imageW, height: imageH)
    }

    /// 开始加载
    public func loading() {
        loadingImage.displayGIF(data: nil, images: gifImages, repeatCount: .max, duration: duration ?? 2)
    }

    /// 停止加载
    public func endLoading() {
        loadingImage.removePlayGIF()
    }

    override public func removeFromSuperview() {
        loadingImage.removePlayGIF()
        loadingImage.removeFromSuperview()
    }
}

public class PQLoadingHUB: NSObject {
    public static let shared = PQLoadingHUB()
    public let viewTag = 11111
    public var isLoading: Bool = false

    public func showHUB() {
        DispatchQueue.main.async { [weak self] in
            let window = UIApplication.shared.keyWindow
            if (window?.viewWithTag(self!.viewTag)) == nil {
                let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
                loadingHUB.tag = self!.viewTag
                window?.addSubview(loadingHUB)
                loadingHUB.center = window?.center as! CGPoint
                loadingHUB.loading()
                self?.isLoading = true
            }
        }
    }

    public func dismissHUB() {
        DispatchQueue.main.async { [weak self] in
            let window = UIApplication.shared.keyWindow
            if (window?.viewWithTag(self!.viewTag)) != nil {
                window?.viewWithTag(self!.viewTag)?.removeFromSuperview()
                self?.isLoading = false
            }
        }
    }

    public func showHUB(superView: UIView, isVerticality: Bool = false) {
        DispatchQueue.main.async { [weak self] in
            if superView.viewWithTag(self!.viewTag) == nil {
                let hubW: CGFloat = 100
                let supW: CGFloat = superView.frame.width
                let supH: CGFloat = superView.frame.height
                let hubY: CGFloat = isVerticality ? ((supW - hubW) / 2) : ((supH - hubW) / 2)
                let hubX: CGFloat = isVerticality ? ((supH - hubW) / 2) : ((supW - hubW) / 2)
                let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: hubX, y: hubY, width: 100, height: 100))
                loadingHUB.tag = self!.viewTag
                superView.addSubview(loadingHUB)
                loadingHUB.loading()
                self?.isLoading = true
            }
        }
    }

    public func dismissHUB(superView: UIView) {
        DispatchQueue.main.async { [weak self] in
            if superView.viewWithTag(self!.viewTag) != nil {
                superView.viewWithTag(self!.viewTag)?.removeFromSuperview()
                self?.isLoading = false
            }
        }
    }

    override private init() {
        super.init()
    }

    override public func copy() -> Any {
        return self
    }

    override public func mutableCopy() -> Any {
        return self
    }
}
