//
//  SaveSuccessTipsView.swift
//  PhysicalWallPaper
//
//  Created by nkl on 2024/11/8.
//

import Foundation
import Photos
import UIKit

class SaveSuccessTipsView: UIView {
    lazy var iconView: UIImageView = .simpleImage(imageName: "ic_saved")
    lazy var textLabel = UILabel()
    lazy var viewButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .hexColor("#111111")
        btn.setTitle("View", for: .normal)
        btn.setTitleColor(.hexColor("#6EF4F4"), for: .normal)
        btn.titleLabel?.font = .boldSystemFont16
        btn.layer.cornerRadius = 14
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(openPhotoLibrary), for: .touchUpInside)
        return btn
    }()

    var viewActionHandler: (() -> Void)?

    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: 288, height: 48))

        layer.cornerRadius = 8
        backgroundColor = .white

        addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.leading.equalTo(16)
        }

        textLabel.textColor = UIColor.black
        textLabel.font = .boldSystemFont14
        textLabel.text = "Saved"
        addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(12)
            make.centerY.equalToSuperview()
        }

        addSubview(viewButton)
        viewButton.snp.makeConstraints { make in
            make.width.equalTo(64)
            make.height.equalTo(28)
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-12)
        }
        viewButton.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
    }

    // 打开相册
    @objc func openPhotoLibrary() {
        let photosAppURL = URL(string: "photos-redirect://")!

        if UIApplication.shared.canOpenURL(photosAppURL) {
            UIApplication.shared.open(photosAppURL, options: [:], completionHandler: nil)
        } else {
            print("无法打开照片应用")
        }
    }

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

    @objc func buttonClick(_ sender: UIButton) {
        if let viewActionHandler = viewActionHandler {
            viewActionHandler()
        } else {
//            UIApplication.shared.openPhotoLibrary()
        }
    }

    static var isShowing = false
}

extension SaveSuccessTipsView {
    static func show(at view: UIView?,
                     at topOffset: CGFloat? = nil,
                     viewActionHandler: (() -> Void)? = nil) {
        guard !Self.isShowing, let view = view else {
            return
        }
        Self.isShowing = true
        let tip = SaveSuccessTipsView()
        tip.viewActionHandler = viewActionHandler
        view.addSubview(tip)
        tip.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.size.equalTo(tip.frame.size)
            if let topOffset = topOffset {
                make.top.equalTo(topOffset)
            } else {
                make.bottom.equalTo(-200)
            }
        }
        UIImpactFeedbackGenerator(style: .soft).impactOccurred()

        DispatchQueue.global().asyncAfter(deadline: .now() + 2.5) {
            DispatchQueue.main.async {
                tip.removeFromSuperview()
            }
            Self.isShowing = false
        }
    }
}
