//
//  PQStuckPointLoadingView.swift
//  PQSpeed
//
//  Created by SanW on 2021/5/24.
//  Copyright © 2021 BytesFlow. All rights reserved.
//

import Kingfisher
import UIKit
import BFCommonKit

class PQStuckPointLoadingView: UIView {
    var cancelHandle: ((_ sender: UIButton) -> Void)?
    /// 同步进度显示
    lazy var loadingView: UIImageView = {
        let loadingView = UIImageView()
        loadingView.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        let data = try? Data(contentsOf: URL(fileURLWithPath: currentBundlePath()!.path(forResource: "stuckPoint_edit_loading", ofType: ".gif")!))
        if data != nil {
            PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { _, images, duration in
                loadingView.displayGIF(data: nil, images: images, repeatCount: .max, duration: duration ?? 2)
            }
        }
        return loadingView
    }()

    lazy var navBarLeftBtn: UIButton = {
        let navBarLeftBtn = UIButton(type: .custom)
        navBarLeftBtn.frame = CGRect(x: 0, y: cDevice_iPhoneStatusBarHei, width: cDefaultMargin * 4, height: cDefaultMargin * 4)
        navBarLeftBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0)
        navBarLeftBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0)
        navBarLeftBtn.tintColor = PQBFConfig.shared.styleTitleColor
        navBarLeftBtn.setImage(UIImage.moduleImage(named: "icon_detail_back", moduleName: "BFFramework",isAssets: false)?.withRenderingMode(.alwaysTemplate), for: .normal)
        navBarLeftBtn.addTarget(self, action: #selector(cancelDownload(sender:)), for: .touchUpInside)
        return navBarLeftBtn
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5)
        addSubViews()
        addLayout()
        
    }
    
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }

    func addSubViews() {
    
        addSubview(loadingView)
        addSubview(navBarLeftBtn)
        
    }

    func addLayout() {
        loadingView.snp.makeConstraints { make in
//            make.top.equalToSuperview().offset(cScreenWidth / 2.0 + cDevice_iPhoneStatusBarHei)
            make.centerX.equalTo(cScreenWidth / 2.0)
            make.centerY.equalTo(cScreenHeigth / 2.0)
            make.width.height.equalTo(cDefaultMargin * 10)
        }
        
    }
    
    
    func show() {
        if self.superview != nil {
            return
        }
        UIApplication.shared.keyWindow?.addSubview(self)
        let data = try? Data(contentsOf: URL(fileURLWithPath: currentBundlePath()!.path(forResource: "stuckPoint_edit_loading", ofType: ".gif")!))
        if data != nil {
            PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { _, images, duration in
                self.loadingView.displayGIF(data: nil, images: images, repeatCount: .max, duration: duration ?? 2)
            }
        }
    }

    /// 移除视图
    /// - Returns: <#description#>
    func removeMarskView() {
        if self.superview != nil {
            removeFromSuperview()            
        }
    }

    @objc func cancelDownload(sender: UIButton) {
        if cancelHandle != nil {
            cancelHandle!(sender)
            removeMarskView()
        }
    }

    deinit {
        BFLog(message: "销毁加载中视图")
    }
}
