//
//  PointAndShootShootingTest.swift
//  BeamTests
//
//  Created by Stef Kors on 08/06/2021.
//

import XCTest

import Nimble

@testable import Beam
@testable import BeamCore

class PointAndShootShootingTest: PointAndShootTest {

    override func setUpWithError() throws {
        initTestBed()
        self.pns.mouseLocation = NSPoint(x: 201, y: 202)
        self.testPage?.url = URL(string: TestWebPage.urlStr)
    }

    func testFailShootWithoutActivePointGroup() throws {
        let target: PointAndShoot.Target = PointAndShoot.Target(
            id: UUID().uuidString,
            rect: NSRect(x: 101, y: 102, width: 301, height: 302),
            mouseLocation: NSPoint(x: 201, y: 202),
            beamElements: [BeamElement("Pointed text")],
            animated: false
        )

        XCTAssertNil(self.pns.activeShootGroup)
        self.pns.isAltKeyDown = true
        self.pns.pointShoot(target.id, target, "text string", "https://pnsTest.co")
        XCTAssertNil(self.pns.activeShootGroup)
    }

    func testFailShootWithoutAltKeyDown() throws {
        let target: PointAndShoot.Target = PointAndShoot.Target(
            id: UUID().uuidString,
            rect: NSRect(x: 101, y: 102, width: 301, height: 302),
            mouseLocation: NSPoint(x: 201, y: 202),
            beamElements: [BeamElement("Pointed text")],
            animated: false
        )
        // Point
        self.pns.point(target, "text string", "https://pnsTest.co")
        XCTAssertNil(self.pns.activeShootGroup)
        self.pns.isAltKeyDown = true
        self.pns.pointShoot(target.id, target, "text string", "https://pnsTest.co")
        XCTAssertNotNil(self.pns.activeShootGroup)
    }

    func testShootAndUnshoot() throws {
        let target: PointAndShoot.Target = PointAndShoot.Target(
            id: UUID().uuidString,
            rect: NSRect(x: 101, y: 102, width: 301, height: 302),
            mouseLocation: self.pns.mouseLocation,
            beamElements: [BeamElement("Pointed text")],
            animated: false
        )
        // Point
        self.pns.point(target, "text string", "https://pnsTest.co")
        // Shoot
        XCTAssertNil(self.pns.activeShootGroup)
        self.pns.isAltKeyDown = true
        self.pns.pointShoot(target.id, target, "text string", "https://pnsTest.co")
        XCTAssertNotNil(self.pns.activeShootGroup)
    }

    func testDismissedShoot() throws {
        let target: PointAndShoot.Target = PointAndShoot.Target(
            id: UUID().uuidString,
            rect: NSRect(x: 101, y: 102, width: 301, height: 302),
            mouseLocation: self.pns.mouseLocation,
            beamElements: [BeamElement("Pointed text")],
            animated: false
        )
        // Point
        self.pns.point(target, "text string", "https://pnsTest.co")
        // Shoot
        XCTAssertNil(self.pns.activeShootGroup)
        self.pns.isAltKeyDown = true
        self.pns.pointShoot(target.id, target, "text string", "https://pnsTest.co")
        XCTAssertNotNil(self.pns.activeShootGroup)
        XCTAssertEqual(self.pns.dismissedGroups.count, 0)
        self.pns.cancelShoot()
        XCTAssertNil(self.pns.activeShootGroup)
        XCTAssertEqual(self.pns.dismissedGroups.count, 1)
    }

    func testCompletedShoot() throws {
        let target: PointAndShoot.Target = PointAndShoot.Target(
            id: UUID().uuidString,
            rect: NSRect(x: 101, y: 102, width: 301, height: 302),
            mouseLocation: self.pns.mouseLocation,
            beamElements: [BeamElement("Pointed text")],
            animated: false
        )
        let url = "https://pnsTest.co"
        self.testPage?.url = URL(string: url)
        // Point
        self.pns.point(target, "text string", url)
        // Shoot
        XCTAssertNil(self.pns.activeShootGroup)
        self.pns.isAltKeyDown = true
        self.pns.pointShoot(target.id, target, "text string", url)
        XCTAssertNotNil(self.pns.activeShootGroup)

        guard let testPage = self.testPage else {
            XCTFail("testpage required for shoot test")
            return
        }

        if let group = self.pns.activeShootGroup {
            self.pns.addShootToNote(targetNote: testPage.activeNote, group: group)
            XCTAssertEqual(self.testPage?.events.count, 2)
            XCTAssertEqual(self.testPage?.events[1], "addContent \(url)")
        }
    }
}
