package premium_store;

import premium_store.model.GameClient;
import premium_store.model.TankLevel;
import premium_store.model.Nation;
import premium_store.model.Tank;
import premium_store.service.GameClientService;
import premium_store.service.TankLevelService;
import premium_store.service.NationService;
import premium_store.service.TankService;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import premium_store.service.exception.ClientNotFoundException;

import java.util.*;

@SpringBootTest
class PremiumStoreApplicationTests {
	private static final Logger log = LoggerFactory.getLogger(PremiumStoreApplicationTests.class);

	@Autowired
	TankService tankService;

	@Autowired
	GameClientService gameClientService;

	@Autowired
	TankLevelService tankLevelService;

	@Autowired
	NationService nationService;

	@Test
	void testClientRead() {
		gameClientService.deleteAllClients();

		GameClient client = gameClientService.addClient("3tankista73", "fff@mail.ru", 3400);
		log.info(client.toString());

		GameClient findClient = gameClientService.findClient(client.getId());
		log.info(findClient.toString());

		Assertions.assertEquals(client, findClient);
	}

	@Test
	void testNationRead() {
		nationService.deleteAllNations();
		tankService.deleteAllTanks();
		tankLevelService.deleteAllLevels();

		Nation nation = nationService.addNation("Германия");

		TankLevel firstLevel = tankLevelService.addLevel(8);
		TankLevel secondLevel = tankLevelService.addLevel(9);

		Tank firstTank =  tankService.addTank("ИС-3", nation, firstLevel, 3700000);
		Tank secondTank = tankService.addTank("E-75", nation, secondLevel, 5600000);

		List<Tank> tanks = new ArrayList<>(Arrays.asList(firstTank, secondTank));

		nation = nationService.updateNation(nation.getId(), "СССР", null);

		log.info(nation.toString());

		Nation findNation = nationService.findNation(nation.getId());
		log.info(findNation.toString());

		Assertions.assertEquals(nation, findNation);
	}

	@Test
	void testClientReadNotFound() {
		gameClientService.deleteAllClients();
		Assertions.assertThrows(ClientNotFoundException.class, () -> gameClientService.findClient(-1L));
	}

	@Test
	void testLevelRead() {
		tankService.deleteAllTanks();
		tankLevelService.deleteAllLevels();

		TankLevel level = tankLevelService.addLevel(8);
		log.info(level.toString());

		TankLevel secondLevel = tankLevelService.addLevel(9);
		log.info(secondLevel.toString());

		Assertions.assertEquals(tankLevelService.findAllLevels().size(), 2);
	}

	@Test
	void testLevelReadAllEmpty() {
		tankService.deleteAllTanks();
		tankLevelService.deleteAllLevels();

		List<TankLevel> levels = tankLevelService.findAllLevels();
		log.info(levels.toString());

		Assertions.assertEquals(levels.size(), 0);
	}

	@Test
	void testTankReadAll() {
		tankService.deleteAllTanks();
		tankLevelService.deleteAllLevels();

		TankLevel firstLevel = tankLevelService.addLevel(8);
		TankLevel secondLevel = tankLevelService.addLevel(9);

		tankService.addTank("ИС-3", nationService.addNation("СССР"), firstLevel, 3700000);
		tankService.addTank("E-75", nationService.addNation("Германия"), secondLevel, 5600000);

		List<Tank> tanks = tankService.findAllTanks();
		log.info(tanks.toString());

		Assertions.assertEquals(tanks.size(), 2);
	}

	@Test
	void testClientCreate() {
		gameClientService.deleteAllClients();

		GameClient firstClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
		log.info(firstClient.toString());

		GameClient secondClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
		log.info(secondClient.toString());

		Assertions.assertEquals(gameClientService.findAllClients().size(), 2);
	}

	@Test
	void testUpdateTank(){
		tankService.deleteAllTanks();
		tankLevelService.deleteAllLevels();
		gameClientService.deleteAllClients();

		TankLevel firstLevel = tankLevelService.addLevel(8);
		TankLevel secondLevel = tankLevelService.addLevel(9);

		Tank firstTank =  tankService.addTank("ИС-3", nationService.addNation("СССР"), firstLevel, 3700000);
		Tank secondTank = tankService.addTank("E-75", nationService.addNation("Германия"), secondLevel, 5600000);

		Tank tank = tankService.findTank(firstTank.getId());
		log.info(tank.toString());

		tank.setName("ИСУ-152");
		tank.setLevel(secondLevel);
		tank.setCost(4100000);

		log.info(tank.toString());
	}

	@Test
	void testNewTest() {
		nationService.deleteAllNations();
		tankService.deleteAllTanks();
		tankLevelService.deleteAllLevels();

		Nation nation = nationService.addNation("Германия");
		Nation newNation = nationService.addNation("CCCР");

		TankLevel firstLevel = tankLevelService.addLevel(8);
		TankLevel secondLevel = tankLevelService.addLevel(9);

		Tank firstTank =  tankService.addTank("ИС-3", nation, firstLevel, 3700000);
		Tank secondTank = tankService.addTank("E-75", nation, secondLevel, 5600000);

		List<Tank> tanks = new ArrayList<>(Arrays.asList(firstTank, secondTank));

		nation = nationService.updateNation(nation.getId(), "СССР", null);

		//tanks = tankService.findListTank(firstLevel, nation);

		log.info("AAAAA" + tanks.toString());
	}
}
