; ============================================================================
; DataModel Module - Data Structures and Utilities
; ============================================================================

Module DataModel
	EnableExplicit
	
	; -- Private Procedure Declarations --
	Declare LoadMimeTypes()
	
	; ========================================================================
	; Response Parsing Utilities
	; ========================================================================
	
	Procedure.s StripPrefix(Result.s, Prefix.s)
		ProcedureReturn RemoveString(Result, Prefix, #PB_String_CaseSensitive, 0, 1)
	EndProcedure
	
	Procedure.s GetField(Record.s, Index)
		ProcedureReturn StringField(Record, Index, General::#FieldSeparator)
	EndProcedure
	
	Procedure GetRecordCount(Result.s)
		ProcedureReturn CountString(Result, General::#RecordSeparator)
	EndProcedure
	
	Procedure.s GetRecord(Result.s, Index)
		ProcedureReturn StringField(Result, Index, General::#RecordSeparator)
	EndProcedure
	
	Procedure.b ValidateResponse(Result.s, ExpectedPrefix.s)
		ProcedureReturn Bool(Left(Result, Len(ExpectedPrefix)) = ExpectedPrefix)
	EndProcedure
	
	; ========================================================================
	; Formatting for List Display
	; ========================================================================
	
	Procedure.s FormatArticleListItem(*Article.Article)
		Protected Status.s
		
		If *Article\Draft
			Status = "Draft"
		Else
			Status = "Published"
		EndIf
		
		ProcedureReturn *Article\Title + Chr(10) + Status + Chr(10) + FormatDate("%yy-%mm-%dd", *Article\Date)
	EndProcedure
	
	Procedure.s FormatTagListItem(*Tag.Tag)
		ProcedureReturn *Tag\DefaultName + Chr(10) + *Tag\Color
	EndProcedure
	
	; ========================================================================
	; Lookup Functions
	; ========================================================================
	
	Procedure FindArticleByID(ID)
		ForEach Articles()
			If Articles()\ID = ID
				ProcedureReturn #True
			EndIf
		Next
		ProcedureReturn #False
	EndProcedure
	
	Procedure FindFileByID(ID)
		ForEach Files()
			If Files()\ID = ID
				ProcedureReturn #True
			EndIf
		Next
		ProcedureReturn #False
	EndProcedure
	
	Procedure FindTagByID(ID)
		ForEach Tags()
			If Tags()\ID = ID
				ProcedureReturn #True
			EndIf
		Next
		ProcedureReturn #False
	EndProcedure
	
	; ========================================================================
	; MIME Type Loading
	; ========================================================================
	
	Procedure LoadMimeTypes()
		Protected.s Ext, Type
		
		Restore MimeData
		Read.s Ext
		While Ext <> "END"
			Read.s Type
			MIMETypes(Ext) = Type
			Read.s Ext
		Wend
	EndProcedure
	
	; -- Module Initialization --
	LoadMimeTypes()
	
	; ========================================================================
	; MIME Type Data
	; ========================================================================
	
	DataSection
		MimeData:
		Data.s "aac", "audio/aac"
		Data.s "abw", "application/x-abiword"
		Data.s "apng", "image/apng"
		Data.s "avi", "video/x-msvideo"
		Data.s "bin", "application/octet-stream"
		Data.s "bmp", "image/bmp"
		Data.s "css", "text/css"
		Data.s "csv", "text/csv"
		Data.s "doc", "application/msword"
		Data.s "gif", "image/gif"
		Data.s "htm", "text/html"
		Data.s "html", "text/html"
		Data.s "ico", "image/x-icon"
		Data.s "jpeg", "image/jpeg"
		Data.s "jpg", "image/jpeg"
		Data.s "js", "text/javascript"
		Data.s "json", "application/json"
		Data.s "mp3", "audio/mpeg"
		Data.s "mp4", "video/mp4"
		Data.s "mpeg", "video/mpeg"
		Data.s "otf", "font/otf"
		Data.s "png", "image/png"
		Data.s "pdf", "application/pdf"
		Data.s "php", "application/x-httpd-php"
		Data.s "svg", "image/svg+xml"
		Data.s "txt", "text/plain"
		Data.s "wav", "audio/wav"
		Data.s "webm", "video/webm"
		Data.s "webp", "image/webp"
		Data.s "woff", "font/woff"
		Data.s "woff2", "font/woff2"
		Data.s "xml", "application/xml"
		Data.s "zip", "application/zip"
		Data.s "END"
	EndDataSection
EndModule

; IDE Options = SpiderBasic 3.10 (Linux - x64)
; CursorPosition = 9
; FirstLine = 42
; Folding = BA-
; iOSAppOrientation = 0
; AndroidAppCode = 0
; AndroidAppOrientation = 0
; EnableXP
; DPIAware
; CompileSourceDirectory