<?php

use PHPUnit\Framework\TestCase;
include_once '../src/IceStorm.class.php';

class IceStormTest extends TestCase {

	protected function setUp():void {
		$this->iceStorm=new IceStorm();
		$this->imagePath=realpath(__DIR__.'/testVisibleImage.jpg');
		self::assertFileExists($this->imagePath);
	}

	protected function tearDown():void {
	}

	public function testGetConfig(){
		$config=$this->iceStorm->getConfig();
		self::assertIsArray($config);
		self::assertArrayHasKey('apiKey',$config);
	}

	public function testQueueUnscoredImage() {
		$config=$this->iceStorm->getConfig();
		self::assertFileExists($this->imagePath);
		$jsonPath=$this->iceStorm->queueUnscoredImage($this->imagePath);
		self::assertFileExists($jsonPath);
		$metadata=json_decode(file_get_contents($jsonPath), true);
		self::assertIsArray($metadata);
		self::assertArrayHasKey('clientImageId', $metadata);
	}

	public function testQueueUnscoredImageWithClientId() {
		$config=$this->iceStorm->getConfig();
		self::assertFileExists($this->imagePath);
		$jsonPath=$this->iceStorm->queueUnscoredImage($this->imagePath, 69420);
		self::assertFileExists($jsonPath);
		$metadata=json_decode(file_get_contents($jsonPath), true);
		self::assertIsArray($metadata);
		self::assertArrayHasKey('clientImageId', $metadata);
		self::assertEquals(69420, $metadata['clientImageId']);
	}

	public function testQueueUnscoredImageWithMetadata() {
		$config=$this->iceStorm->getConfig();
		self::assertFileExists($this->imagePath);
		$jsonPath=$this->iceStorm->queueUnscoredImage($this->imagePath, 0, ['ownerid'=>321]);
		self::assertFileExists($jsonPath);
		$metadata=json_decode(file_get_contents($jsonPath), true);
		self::assertIsArray($metadata);
		self::assertArrayHasKey('clientMetadata', $metadata);
		self::assertArrayHasKey('ownerid', $metadata['clientMetadata']);
		self::assertEquals(321, $metadata['clientMetadata']['ownerid']);
	}

	public function testQueueUnscoredImageNonExistentImage() {
		$config=$this->iceStorm->getConfig();
		self::assertFileDoesNotExist($this->imagePath.'.bogus');
		self::expectExceptionMessageMatches('/Image does not exist/');
		$jsonPath=$this->iceStorm->queueUnscoredImage($this->imagePath.'.bogus');
	}

	public function testQueueHumanScoredImage() {
		$config=$this->iceStorm->getConfig();
		self::assertFileExists($this->imagePath);
		$jsonPath=$this->iceStorm->queueHumanScoredImage($this->imagePath, 'Crystals');
		self::assertFileExists($jsonPath);
		$metadata=json_decode(file_get_contents($jsonPath), true);
		self::assertIsArray($metadata);
		self::assertArrayHasKey('clientImageId', $metadata);
	}

	public function testQueueHumanscoredImageWithClientId() {
		$config=$this->iceStorm->getConfig();
		self::assertFileExists($this->imagePath);
		$jsonPath=$this->iceStorm->queueHumanScoredImage($this->imagePath, 'Heavy Precipitate',69420);
		self::assertFileExists($jsonPath);
		$metadata=json_decode(file_get_contents($jsonPath), true);
		self::assertIsArray($metadata);
		self::assertArrayHasKey('clientImageId', $metadata);
		self::assertEquals(69420, $metadata['clientImageId']);
	}

	public function testQueueHumanScoredImageWithMetadata() {
		$config=$this->iceStorm->getConfig();
		self::assertFileExists($this->imagePath);
		$jsonPath=$this->iceStorm->queueHumanScoredImage($this->imagePath, 'Dust', 0, ['ownerid'=>321]);
		self::assertFileExists($jsonPath);
		$metadata=json_decode(file_get_contents($jsonPath), true);
		self::assertIsArray($metadata);
		self::assertArrayHasKey('clientMetadata', $metadata);
		self::assertArrayHasKey('ownerid', $metadata['clientMetadata']);
		self::assertEquals(321, $metadata['clientMetadata']['ownerid']);
	}

	public function testQueueHumanScoredImageNonExistentImage() {
		$config=$this->iceStorm->getConfig();
		self::assertFileDoesNotExist($this->imagePath.'.bogus');
		self::expectExceptionMessageMatches('/Image does not exist/');
		$jsonPath=$this->iceStorm->queueHumanScoredImage($this->imagePath.'.bogus', "Clear");
	}

	public function testSubmitUnscoredImages() {
//		$jsonPath=$this->iceStorm->queueUnscoredImage($this->imagePath);
		$jsonPath=$this->iceStorm->queueUnscoredImage("C:\\Users\\edaniel\\Downloads\\b1gjef-games-coin.gif");
		$jsonPath=$this->iceStorm->queueUnscoredImage("C:\\Users\\edaniel\\Downloads\\b1gjef-games-coin.gif");
		self::assertFileExists($jsonPath);
		$this->iceStorm->submitUnscoredImages();
	}
	
}