; ============================================================================
; General Module - Data Loading and Initialization
; ============================================================================

Module General
	EnableExplicit
	
	; -- Loading State --
	Enumeration LoadingStages
		#Load_Files
		#Load_Tags
		#Load_Articles
		#Load_Languages
	EndEnumeration
	
	#TotalLoadingStages = 4
	
	Global LoadingProgress = 0
	
	; -- Private Procedure Declarations --
	Declare LoadingCallback(Success, Result.s, UserData)
	Declare ParseFiles(Result.s)
	Declare ParseArticles(Result.s)
	Declare ParseLanguages(Result.s)
	Declare ParseTags(Result.s)
	
	; ========================================================================
	; Initialization
	; ========================================================================
	
	Procedure Init()
		; Load all required data from server in parallel
		HTTPRequest(#PB_HTTP_Get, #Address + "admin/", "listfiles", @LoadingCallback(), #Load_Files)
		HTTPRequest(#PB_HTTP_Get, #Address + "admin/", "listtags", @LoadingCallback(), #Load_Tags)
		HTTPRequest(#PB_HTTP_Get, #Address + "admin/", "listlanguages", @LoadingCallback(), #Load_Languages)
		HTTPRequest(#PB_HTTP_Get, #Address + "admin/", "listarticles", @LoadingCallback(), #Load_Articles)
	EndProcedure
	
	; ========================================================================
	; Loading Callbacks
	; ========================================================================
	
	Procedure LoadingCallback(Success, Result.s, UserData)
		If Not Success
			Debug "[Error] Failed to load data for stage: " + Str(UserData)
			ProcedureReturn
		EndIf
		
		Select UserData
			Case #Load_Files
				ParseFiles(Result)
			Case #Load_Tags
				ParseTags(Result)
			Case #Load_Articles
				ParseArticles(Result)
			Case #Load_Languages
				ParseLanguages(Result)
		EndSelect
		
		LoadingProgress + 1
		If LoadingProgress = #TotalLoadingStages
			MainWindow::Open()
		EndIf
	EndProcedure
	
	; ========================================================================
	; Data Parsers
	; ========================================================================
	
	Procedure ParseFiles(Result.s)
		Protected Count, i, Entry.s
		
		Result = DataModel::StripPrefix(Result, #Prefix_ListFiles)
		Count = DataModel::GetRecordCount(Result)
		
		For i = 1 To Count
			Entry = DataModel::GetRecord(Result, i)
			AddElement(DataModel::Files())
			DataModel::Files()\ID = Val(DataModel::GetField(Entry, 1))
			DataModel::Files()\Name = DataModel::GetField(Entry, 2)
			DataModel::Files()\Type = DataModel::MIMETypes(GetExtensionPart(DataModel::Files()\Name))
		Next
	EndProcedure
	
	Procedure ParseArticles(Result.s)
		Protected Count, i, Entry.s
		
		Result = DataModel::StripPrefix(Result, #Prefix_ListArticles)
		Count = DataModel::GetRecordCount(Result)
		
		For i = 1 To Count
			Entry = DataModel::GetRecord(Result, i)
			AddElement(DataModel::Articles())
			DataModel::Articles()\ID = Val(DataModel::GetField(Entry, 1))
			DataModel::Articles()\Title = DataModel::GetField(Entry, 2)
			DataModel::Articles()\Draft = Val(DataModel::GetField(Entry, 3))
			DataModel::Articles()\Tags = Val(DataModel::GetField(Entry, 4))
			DataModel::Articles()\Date = Val(DataModel::GetField(Entry, 5))
		Next
	EndProcedure
	
	Procedure ParseLanguages(Result.s)
		Protected Count, i
		
		Result = DataModel::StripPrefix(Result, #Prefix_ListLanguages)
		Count = DataModel::GetRecordCount(Result)
		
		For i = 1 To Count
			AddElement(DataModel::Language())
			DataModel::Language() = DataModel::GetRecord(Result, i)
		Next
	EndProcedure
	
	Procedure ParseTags(Result.s)
		Protected Count, i, Entry.s
		
		Result = DataModel::StripPrefix(Result, #Prefix_ListTags)
		Count = DataModel::GetRecordCount(Result)
		
		For i = 1 To Count
			Entry = DataModel::GetRecord(Result, i)
			AddElement(DataModel::Tags())
			DataModel::Tags()\ID = Val(DataModel::GetField(Entry, 1))
			DataModel::Tags()\DefaultName = DataModel::GetField(Entry, 2)
			DataModel::Tags()\Color = DataModel::GetField(Entry, 3)
			DataModel::Tags()\BinaryID = Pow(2, ListIndex(DataModel::Tags()))
		Next
	EndProcedure
	
EndModule

; IDE Options = SpiderBasic 3.10 (Linux - x64)
; CursorPosition = 128
; Folding = B+
; iOSAppOrientation = 0
; AndroidAppCode = 0
; AndroidAppOrientation = 0
; EnableXP
; DPIAware
; CompileSourceDirectory