#import "TQSignResult.h"

@implementation TQSignResult

- (instancetype)initWithRawUrl:(NSString *)rawUrl
                    customPath:(NSString *)customPath
                        flowId:(NSString *)flowId
                        action:(NSString *)action
                        result:(NSString *)result
                          from:(NSString *)from
                        extras:(NSDictionary<NSString *,NSString *> *)extras {
    if (self = [super init]) {
        _rawUrl = [rawUrl copy] ?: @"";
        _customPath = [customPath copy] ?: @"";
        _flowId = [flowId copy] ?: @"";
        _action = [action copy] ?: @"";
        _result = [result copy] ?: @"";
        _from = [from copy] ?: @"";
        _extras = [extras copy] ?: @{};
    }
    return self;
}

+ (TQSignResult *)empty {
    static TQSignResult *empty;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        empty = [[TQSignResult alloc] initWithRawUrl:@""
                                          customPath:@""
                                              flowId:@""
                                              action:@""
                                              result:@""
                                                from:@""
                                              extras:@{}];
    });
    return empty;
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone {
    // 不可变对象,直接返回自身
    return self;
}

#pragma mark - Equality

- (BOOL)isEqual:(id)object {
    if (self == object) return YES;
    if (![object isKindOfClass:[TQSignResult class]]) return NO;
    TQSignResult *o = (TQSignResult *)object;
    return [_rawUrl isEqualToString:o.rawUrl]
        && [_customPath isEqualToString:o.customPath]
        && [_flowId isEqualToString:o.flowId]
        && [_action isEqualToString:o.action]
        && [_result isEqualToString:o.result]
        && [_from isEqualToString:o.from]
        && [_extras isEqualToDictionary:o.extras];
}

- (NSUInteger)hash {
    return _rawUrl.hash ^ _customPath.hash ^ _flowId.hash ^ _action.hash
         ^ _result.hash ^ _from.hash ^ _extras.hash;
}

- (NSString *)description {
    return [NSString stringWithFormat:
            @"<TQSignResult action=%@ result=%@ flowId=%@ customPath=%@ from=%@ extras=%@>",
            _action, _result, _flowId, _customPath, _from, _extras];
}

@end
