//
//  Entry.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/9.
//

import Foundation
import SwiftEntryKit
import DDAutoUIKit_Private

public let ENTRY = Entry.default

open class Entry {
    public static let `default` = Entry()
    
    func showIdentityAlert(view: UIView, name: String? = nil) {
        var attributes = EKAttributes.centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        
        // ✅ 背景加半透明遮罩
        attributes.screenBackground = .color(
            color: EKColor(UIColor.black.withAlphaComponent(0.45))
        )
        
        // entry 自己画内容，所以这里透明
        attributes.entryBackground = .color(color: .clear)
        
        attributes.screenInteraction = .absorbTouches   // 必须点按钮才能关闭
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )

        // ✅ 让 entry 本身按内容高度来，而不是铺满全屏
        attributes.positionConstraints.size = .init(
            width: .offset(value: 40),   // 左右各留 20，自己可调
            height: .intrinsic           // 高度跟 view 的布局走
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showOnlineVehiclesEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .clear)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .fill,
            height: .fill
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    public func showRefuseOrderConfirmEntry(view:UIView,name:String? = nil){
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .constant(value: auto(280)),
            height: .constant(value: auto(130))
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    public func showRefuseOrderEntry(view:UIView,name:String? = nil){
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .high, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .constant(value: auto(326)),
            height: .constant(value: auto(285))
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showAppUpdateEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .high, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .constant(value: auto(326)),
            height: .constant(value: auto(180))
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showAcceptOrderEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .low, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        
        attributes.positionConstraints = .init(verticalOffset:0,size:.init(width:EKAttributes.PositionConstraints.Edge.constant(value: auto(276)),height:EKAttributes.PositionConstraints.Edge.constant(value: auto(148))))

        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showPushNotiCommonEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        
        attributes.positionConstraints = .init(verticalOffset:0,size:.init(width:EKAttributes.PositionConstraints.Edge.constant(value: auto(276)),height:EKAttributes.PositionConstraints.Edge.constant(value: auto(148))))

        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showNotificationAuthEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .constant(value: auto(280)),
            height: .constant(value: auto(130))
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    public func showVehicleLogoutEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .constant(value: auto(280)),
            height: .constant(value: auto(130))
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showUserPermissionsEntry(view:UIView,name:String? = nil) {
        var attributes = EKAttributes()
        attributes = .centerFloat
        attributes.name = name
        attributes.precedence = .override(priority: .normal, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .clear)
        attributes.entryBackground = .color(color: .white)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .constant(value: auto(280)),
            height: .constant(value: auto(130))
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showVehicleMonitoringConfigEntry(view: UIView, name: String? = nil) {
        var attributes = EKAttributes()
        attributes = .bottomFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .black.with(alpha: 0.3))
        attributes.entryBackground = .color(color: .clear)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .fill,
            height: .fill
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes)
    }
    
    func showSearchEntry(view: UIView, name: String? = nil, bottomHeight: CGFloat) {
        var attributes = EKAttributes()
        attributes = .bottomFloat
        attributes.name = name
        attributes.precedence = .override(priority: .min, dropEnqueuedEntries: false)
        attributes.displayMode = .inferred
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .black.with(alpha: 0.3))
        attributes.entryBackground = .color(color: .clear)
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .absorbTouches
        attributes.scroll = .disabled
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.25,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.exitAnimation = .init(
            translate: .init(duration: 0.25)
        )
        attributes.popBehavior = .animated(
            animation: .init(
                translate: .init(duration: 0.25)
            )
        )
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 6
            )
        )
        attributes.positionConstraints.size = .init(
            width: .fill,
            height: .constant(value: auto(450) + bottomHeight)
        )
        attributes.positionConstraints.verticalOffset = 0
        attributes.positionConstraints.safeArea = .overridden
        attributes.positionConstraints.rotation.isEnabled = false
        attributes.statusBar = .light
        SwiftEntryKit.display(entry: view, using: attributes, presentInsideKeyWindow: true)
    }
    
    public func isCurrentlyDisplaying(entryNamed name: String? = nil) -> Bool {
        return SwiftEntryKit.isCurrentlyDisplaying(entryNamed: name)
    }
    
    public func dismiss(name:String? = nil,completion:(() -> Void)? = nil) {
        if let name {
            SwiftEntryKit.dismiss(.specific(entryName: name),with: completion)
        }else{
            SwiftEntryKit.dismiss(with: completion)
        }
    }
}
