#import "VolcApiEngine/VePictureInPictureController.h"
#import <AVKit/AVKit.h>
#import <Foundation/Foundation.h>
#import <TTSDKFramework/TTSDKFramework.h>

NS_ASSUME_NONNULL_BEGIN

@class TTVideoEngine;
@class UIView;
@class PlayerMultiObserver;

// Forward declaration
typedef struct EngineVideoWrapper EngineVideoWrapper;

@protocol PictureInPictureManagerListener <NSObject>
@optional
- (void)onStartPictureInPicture;
- (void)onStopPictureInPicture;
- (void)onClickPictureInPictureRestoreBtn;
- (void)onError:(NSInteger)code extraData:(NSDictionary *)extraData;
- (void)onPictureInPictureStateUpdate:(VePictureInPictureStatus)state
                              message:(nullable NSString *)message;
@end

@interface PictureInPictureManager
    : NSObject <VePictureInPictureDelegate,
                AVPictureInPictureControllerDelegate,
                AVPictureInPictureSampleBufferPlaybackDelegate>
@property(nonatomic, assign) VePictureInPictureType type;

// Current state
@property(nonatomic, assign, readonly) VePictureInPictureStatus state;

// Configure whether to support automatic picture-in-picture in background
// Default is NO
@property(nonatomic, assign)
    BOOL canStartPictureInPictureAutomaticallyFromInline API_AVAILABLE(ios(14.2)
    );

// Whether to automatically hide the current player's view controller after
// starting picture-in-picture, default is NO
@property(nonatomic, assign) BOOL autoHideViewController;

/**
 * 是否同步播放器视图配置到画中画窗口
 * 设置为 YES 时，画中画窗口将使用播放器视图的宽高比和填充模式(renderMode)。
 * 此配置对手动启动和自动启动画中画均生效。
 * 建议在 enableAutoStartPictureInPicture 或 startPictureInPicture 之前设置。
 * @default NO
 */
@property(nonatomic, assign) BOOL syncPlayerViewConfig;

/**
 * Set whether to support automatic picture-in-picture from inline mode
 * @param canStartPictureInPictureAutomaticallyFromInline Whether to allow
 * automatic picture-in-picture, default is NO
 * @note This feature requires iOS 14.2 and above
 * @since iOS 14.2
 */
- (void)setCanStartPictureInPictureAutomaticallyFromInline:
    (BOOL)canStartPictureInPictureAutomaticallyFromInline
    API_AVAILABLE(ios(14.2));

// New public initialization method
- (instancetype)init;

// Expose other methods that need to be called from RN
- (void)setupPlayer:(TTVideoEngine *)player;
- (void)setupPlayer:(TTVideoEngine *)player
    withMultiObserver:(nullable PlayerMultiObserver *)multiObserver;
- (void)setupPlayer:(TTVideoEngine *)player
         completion:(nullable void (^)(void))completion;
- (BOOL)startPictureInPicture;
- (BOOL)startPictureInPictureWithPlayer:(TTVideoEngine *)player;
- (void)startPictureInPictureWithPlayer:(TTVideoEngine *)player
                             completion:
                                 (nullable void (^)(BOOL success))completion;

/**
 * 启动画中画（带同步播放器视图配置选项）
 * @param player 播放器实例
 * @param syncPlayerViewConfig 是否同步播放器视图配置（尺寸和填充模式）
 * @param completion 完成回调
 */
- (void)startPictureInPictureWithPlayer:(TTVideoEngine *)player
                    syncPlayerViewConfig:(BOOL)syncPlayerViewConfig
                             completion:(nullable void (^)(BOOL success))completion;
- (void)stopPictureInPictureWithRestore:(BOOL)restore;
- (void)stopPictureInPicture;
- (void)destroyPictureInPicture;
- (BOOL)isPictureInPictureStarted;
- (BOOL)isPictureInPictureSupported;
- (void)setListener:(id<PictureInPictureManagerListener>)listener;
- (void)ensurePipControllerReady:(TTVideoEngine *)player
                      completion:(nullable void (^)(void))completion;

// 设置视频尺寸（带完成回调）
- (void)setVideoSizeWithCompletion:(nullable void (^)(void))completion;

/**
 * Enable automatic picture-in-picture feature
 *
 * This method is used to enable automatic picture-in-picture when the app exits
 * to background. When enabled, the system will automatically start
 * picture-in-picture mode when the app switches to background or when the user
 * presses the home button, without requiring manual trigger.
 *
 * @param player The player instance to set, used to create pipController in
 * advance
 * @note This feature requires iOS 14.2 and above
 * @warning Please ensure the app has proper picture-in-picture permissions
 * before calling this method
 * @since iOS 14.2
 */
- (void)enableAutoStartPictureInPicture:(TTVideoEngine *)player
    API_AVAILABLE(ios(14.2));

/**
 * Disable automatic picture-in-picture feature
 *
 * This method is used to disable automatic picture-in-picture when the app
 * exits to background. When disabled, picture-in-picture won't start
 * automatically when the app switches to background, requiring manual user
 * activation. It will also destroy the current picture-in-picture controller.
 *
 * @note This feature requires iOS 14.2 and above
 * @since iOS 14.2
 */
- (void)disableAutoStartPictureInPicture API_AVAILABLE(ios(14.2));

// Video wrapper creation methods
- (EngineVideoWrapper *)createVideoWrapper:(id)managerContext;
- (EngineVideoWrapper *)createVideoWrapperWithEmpty:(id)managerContext;

// Class methods
+ (instancetype)sharedInstance;
+ (instancetype)getInstance;
+ (BOOL)isPictureInPictureSupported;
+ (BOOL)isPictureInPictureStarted;
+ (void)stopPictureInPicture;

/// 同步销毁 PiP，必须在主线程调用。
/// 与 destroyPictureInPicture 不同，此方法不使用 dispatch_async，
/// 保证 PiP 相关 KVO 在调用返回前已完全解绑。
- (void)destroyPictureInPictureSync;

@end

NS_ASSUME_NONNULL_END
