#import "TQSdkEvent.h"

#pragma mark - 1. 页面生命周期

@implementation TQPageStartedEvent
- (instancetype)initWithUrl:(NSString *)url {
    if (self = [super init]) { _url = [url copy] ?: @""; }
    return self;
}
- (NSString *)description { return [NSString stringWithFormat:@"pageStarted(%@)", _url]; }
@end

@implementation TQPageFinishedEvent
- (instancetype)initWithUrl:(NSString *)url {
    if (self = [super init]) { _url = [url copy] ?: @""; }
    return self;
}
- (NSString *)description { return [NSString stringWithFormat:@"pageFinished(%@)", _url]; }
@end

#pragma mark - 2. 权限

@implementation TQPermissionGrantedEvent
- (instancetype)initWithPermissions:(NSArray<NSString *> *)permissions {
    if (self = [super init]) { _permissions = [permissions copy] ?: @[]; }
    return self;
}
- (NSString *)description { return [NSString stringWithFormat:@"permissionGranted(%@)", _permissions]; }
@end

@implementation TQPermissionDeniedEvent
- (instancetype)initWithPermissions:(NSArray<NSString *> *)permissions {
    if (self = [super init]) { _permissions = [permissions copy] ?: @[]; }
    return self;
}
- (NSString *)description { return [NSString stringWithFormat:@"permissionDenied(%@)", _permissions]; }
@end

#pragma mark - 3. Scheme 拦截

@implementation TQSchemeInterceptedEvent
- (instancetype)initWithScheme:(NSString *)scheme url:(NSString *)url {
    if (self = [super init]) {
        _scheme = [scheme copy] ?: @"";
        _url = [url copy] ?: @"";
    }
    return self;
}
- (NSString *)description { return [NSString stringWithFormat:@"schemeIntercepted(%@, %@)", _scheme, _url]; }
@end

@implementation TQSchemeNotMatchedEvent
- (instancetype)initWithUrl:(NSString *)url {
    if (self = [super init]) { _url = [url copy] ?: @""; }
    return self;
}
- (NSString *)description { return [NSString stringWithFormat:@"schemeNotMatched(%@)", _url]; }
@end

#pragma mark - 4. 网络与渲染错误

@implementation TQLoadErrorEvent
- (instancetype)initWithUrl:(NSString *)url
                  errorCode:(NSInteger)errorCode
                description:(NSString *)description {
    if (self = [super init]) {
        _url = [url copy] ?: @"";
        _errorCode = errorCode;
        _errorDescription = [description copy] ?: @"";
    }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"loadError(%@, code=%ld, %@)",
            _url, (long)_errorCode, _errorDescription];
}
@end

@implementation TQHttpErrorEvent
- (instancetype)initWithUrl:(NSString *)url
                 statusCode:(NSInteger)statusCode
               reasonPhrase:(NSString *)reasonPhrase {
    if (self = [super init]) {
        _url = [url copy] ?: @"";
        _statusCode = statusCode;
        _reasonPhrase = [reasonPhrase copy] ?: @"";
    }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"httpError(%@, status=%ld, %@)",
            _url, (long)_statusCode, _reasonPhrase];
}
@end

@implementation TQSslErrorEvent
- (instancetype)initWithUrl:(NSString *)url primaryError:(NSInteger)primaryError {
    if (self = [super init]) {
        _url = [url copy] ?: @"";
        _primaryError = primaryError;
    }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"sslError(%@, primary=%ld)", _url, (long)_primaryError];
}
@end

#pragma mark - 5. 下载

@implementation TQDownloadStartEvent
- (instancetype)initWithUrl:(NSString *)url
                   mimeType:(NSString *)mimeType
         contentDisposition:(NSString *)contentDisposition {
    if (self = [super init]) {
        _url = [url copy] ?: @"";
        _mimeType = [mimeType copy] ?: @"";
        _contentDisposition = [contentDisposition copy] ?: @"";
    }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"downloadStart(%@, mime=%@)", _url, _mimeType];
}
@end

@implementation TQDownloadCompletedEvent
- (instancetype)initWithUrl:(NSString *)url localPath:(NSString *)localPath {
    if (self = [super init]) {
        _url = [url copy] ?: @"";
        _localPath = [localPath copy] ?: @"";
    }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"downloadCompleted(%@ → %@)", _url, _localPath];
}
@end

@implementation TQDownloadFailedEvent
- (instancetype)initWithUrl:(NSString *)url reason:(NSString *)reason {
    if (self = [super init]) {
        _url = [url copy] ?: @"";
        _reason = [reason copy] ?: @"";
    }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"downloadFailed(%@, %@)", _url, _reason];
}
@end

#pragma mark - 6. 渲染进程崩溃

@implementation TQWebContentProcessTerminatedEvent
- (instancetype)initWithUrl:(NSString *)url {
    if (self = [super init]) { _url = [url copy] ?: @""; }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"webContentProcessTerminated(%@)", _url];
}
@end

#pragma mark - 7. UA 注入失败

@implementation TQUserAgentInjectFailedEvent
- (instancetype)initWithReason:(NSString *)reason {
    if (self = [super init]) { _reason = [reason copy] ?: @""; }
    return self;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"userAgentInjectFailed(%@)", _reason];
}
@end
