package bg.car_wash;

import bg.car_wash.areas.car.entity.CarMakeModel;
import bg.car_wash.areas.car.service.CarMakeModelService;
import bg.car_wash.areas.user.entity.User;
import bg.car_wash.areas.user.service.UserService;
import bg.car_wash.configurations.database.InsertCars;
import bg.car_wash.configurations.database.InsertUsers;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.PostConstruct;
import java.io.FileNotFoundException;
import java.util.List;

@SpringBootApplication
public class CarWashApplication {

	@Autowired
	private UserService userService;

	@Autowired
	private CarMakeModelService carMakeModelService;

	public static void main(String[] args) {
		SpringApplication.run(CarWashApplication.class, args);
	}

	@PostConstruct
	public void initUsers() throws FileNotFoundException {
		//create 6 default users in DB
		if (checkUserDbIsEmpty()) {
			InsertUsers userDefault = new InsertUsers();
			for (User user : userDefault.getUsers()) {
				this.userService.createUser(user);
			}
		}

		//create all cars with models and makes in DB
		if(checkCarMakeModelDbIsEmpty()) {
			InsertCars insertCars = new InsertCars();
			List<CarMakeModel> carMakeModels = insertCars.readJsonFile();
			for (CarMakeModel carMakeModel : carMakeModels) {
				this.carMakeModelService.createCar(carMakeModel);
			}
		}
	}

	private boolean checkCarMakeModelDbIsEmpty() {
		if(this.carMakeModelService.findAllCarMakeModel().isEmpty()) {
			return true;
		}

		return false;
	}

	private boolean checkUserDbIsEmpty() {
		if (this.userService.findAllUsers().isEmpty()) {
			return true;
		}

		return false;
	}

	@Bean
	public ModelMapper getModelMapper() {
		return new ModelMapper();
	}

}

@@ -115,32 +115,126 @@
 keModelService;%0A
+import bg.car_wash.areas.role.entity.Role;%0Aimport bg.car_wash.areas.role.service.RoleService;%0A
 import bg.car_wa
@@ -415,21 +415,142 @@
 e.Insert
-Users
+Roles;%0Aimport bg.car_wash.configurations.database.InsertUsers;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder
 ;%0Aimport
@@ -1084,16 +1084,63 @@
 rvice;%0A%0A
+%09@Autowired%0A%09private RoleService roleService;%0A%0A
 %09public 
@@ -1822,16 +1822,375 @@
 %09%09%09%7D%0A%09%09%7D
+%0A%0A%09%09//create all roles in db%0A%09%09if(checkRoleDbIsEmpty()) %7B%0A%09%09%09InsertRoles insertRoles = new InsertRoles();%0A%09%09%09List%3CRole%3E roles = insertRoles.getRoles();%0A%09%09%09for (Role role : roles) %7B%0A%09%09%09%09this.roleService.createRole(role);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%7D%0A%0A%09private boolean checkRoleDbIsEmpty() %7B%0A%09%09if(this.roleService.findAllRole().isEmpty()) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%0A%09%09return false;
 %0A%09%7D%0A%0A%09pr
@@ -2550,10 +2550,157 @@
 ();%0A%09%7D%0A%0A
+%09@Bean%0A%09public Gson getGson() %7B%0A%09%09return new GsonBuilder()%0A%09%09%09%09.excludeFieldsWithoutExposeAnnotation()%0A%09%09%09%09.setPrettyPrinting()%0A%09%09%09%09.create();%0A%09%7D%0A%0A
 %7D%0A

create all Role in database and create Bean Gson