@echo off
setlocal disableDelayedExpansion

:Variables
set "work_project=%1"
set template_project=Empty
set entities_list=EntitiesList.lst
set entities_filter_list=EntitiesFilterList.lst
set "project_path=%~dp0..\.."

if "%work_project%"=="" exit
ren %project_path%\%template_project%Server\%template_project%Server.csproj %work_project%Server.csproj
ren %project_path%\%template_project%Server.sln %work_project%Server.sln
ren %project_path%\%template_project%Server %work_project%Server
call %~dp0..\Core\add_test_project.bat %work_project% >..\..\XUnitDAL.Test\XUnitWebApi.Test.csproj


call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server.sln %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\Program.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\Startup.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\_Shared\SharedControllers\InfoController.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\_Shared\SharedControllers\WebAppUserController.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\_Shared\SharedControllers\WebAppUserHelper.cs %template_project%Server %work_project%Server

call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\_Shared\SharedExtensions\ServiceExtensions.cs             %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\_Shared\SharedExtensions\ServiceSwaggerExtensions.cs      %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\_Shared\SharedExtensions\WebApiMiddlewareOptionsHelper.cs %template_project%Server %work_project%Server

call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\Extends\ServiceExtensions4BaseRepository.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\Extends\JwtMiddlewareOptionsHelper.cs       %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\Extends\JwtMiddlewareOptionsHelper.cs       %template_project% %work_project%

call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\Properties\launchSettings.json %template_project%Server %work_project%Server

call %~dp0..\Core\replaceinfile.bat %project_path%\XUnitDAL.Test\XUnitWebApi.Test.csproj %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\XUnitDAL.Test\_Shared\Shared_Test_Controller_Generic.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\XUnitDAL.Test\_Shared\Shared_Test_LDAP.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\XUnitDAL.Test\_Shared\shared_test_config.cs %template_project% %work_project%
call %~dp0..\Core\replaceinfile.bat %project_path%\XUnitDAL.Test\Test_Controller_Entity.cs %template_project%Server %work_project%Server
call %~dp0..\Core\replaceinfile.bat %project_path%\XUnitDAL.Test\Test_Controller_Filter.cs %template_project%Server %work_project%Server

call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\appsettings.json %template_project% %work_project%
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\AppsettingsFiles\Production\appsettings.json %template_project% %work_project%
call %~dp0..\Core\replaceinfile.bat %project_path%\%work_project%Server\AppsettingsFiles\Develop\appsettings.json %template_project% %work_project%
:Prepare_batch_files
call %~dp0..\Core\sample_entity.bat %work_project% >add_one_entity.bat
if exist add_list_entities.bat del add_list_entities.bat

:Prepare_batch_for_entities
for /F "delims=" %%L in ('findstr /n "^" "%entities_list%"') do (
  set "line=%%L"

  setlocal EnableDelayedExpansion
  set "line=!line:*:=!"
  if "!line!" neq "" echo call ..\Core\start_add_entity_to_project.bat %work_project% !line! >> add_list_entities.bat
  endlocal
)

:Prepare_batch_for_entities_with_filter
for /F "delims=" %%L in ('findstr /n "^" "%entities_filter_list%"') do (
  set "line=%%L"

  setlocal EnableDelayedExpansion
  set "line=!line:*:=!"
  set "entity_filter="
  set "entity="
  for /f "tokens=1,2 delims= " %%a in ("!line!") do set entity=%%a& set entity_filter=%%b
  if "!entity_filter!"=="" set entity_filter=!line!
  if "!entity!" neq "" echo call ..\Core\start_add_entity_to_project.bat %work_project% !entity! !entity_filter! >> add_list_entities.bat
  endlocal
)

if exist add_list_entities.bat call add_list_entities.bat
