#import <XCTest/XCTest.h>
#import "NSUserActivity+WMFExtensions.h"


@interface NSUserActivity_WMFExtensions_wmf_activityForWikipediaScheme_Test : XCTestCase
@end

@implementation NSUserActivity_WMFExtensions_wmf_activityForWikipediaScheme_Test

- (void)testURLWithoutWikipediaSchemeReturnsNil {
    NSURL *url = [NSURL URLWithString:@"http://www.foo.com"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertNil(activity);
}

- (void)testInvalidArticleURLReturnsNil {
    NSURL *url = [NSURL URLWithString:@"wikipedia://en.wikipedia.org/Foo"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertNil(activity);
}

- (void)testArticleURL {
    NSURL *url = [NSURL URLWithString:@"wikipedia://en.wikipedia.org/wiki/Foo"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeLink);
    XCTAssertEqualObjects(activity.webpageURL.absoluteString, @"https://en.wikipedia.org/wiki/Foo");
}

- (void)testExploreURL {
    NSURL *url = [NSURL URLWithString:@"wikipedia://explore"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeExplore);
}

- (void)testHistoryURL {
    NSURL *url = [NSURL URLWithString:@"wikipedia://history"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeHistory);
}

- (void)testSavedURL {
    NSURL *url = [NSURL URLWithString:@"wikipedia://saved"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeSavedPages);
}

- (void)testPlacesURL {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
}

- (void)testPlacesURLWithCoordinates {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=52.370216,4.895168"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssert([activity.userInfo[@"WMFCoordinatesLatitude"] isEqualToNumber:@52.370216]);
    XCTAssert([activity.userInfo[@"WMFCoordinatesLongitude"] isEqualToNumber:@4.895168]);
}

- (void)testPlacesURLWithCoordinatesButNegatives {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=-34.603722,-58.381592"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssert([activity.userInfo[@"WMFCoordinatesLatitude"] isEqualToNumber:@-34.603722]);
    XCTAssert([activity.userInfo[@"WMFCoordinatesLongitude"] isEqualToNumber:@-58.381592]);
}

- (void)testPlacesURLWithCoordinatesButNoLongitude {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=52.370216,"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
}

- (void)testPlacesURLWithCoordinatesButNoLatitude {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=,4.895168"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
}

- (void)testPlacesURLWithCoordinatesButNoCoordinates {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=,"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
}

- (void)testPlacesURLWithCoordinatesButEmpty {
    NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates="];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
    XCTAssertNil(activity.webpageURL);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
    XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
}

- (void)testSearchURL {
    NSURL *url = [NSURL URLWithString:@"wikipedia://en.wikipedia.org/w/index.php?search=dog"];
    NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
    XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeLink);
    XCTAssertEqualObjects(activity.webpageURL.absoluteString,
                          @"https://en.wikipedia.org/w/index.php?search=dog&title=Special:Search&fulltext=1");
}

@end

