import unittest
from datetime import datetime, time

from ecmind_blue_client.ecm.parser._dms_content_parser_hol import DmsContentParserHol
from ecmind_blue_client.ecm.parser._dms_content_parser_lol import DmsContentParserLol


class TestQueryParser(unittest.TestCase):

    def test_hol_query_parser(self):
        with open("tests/testdata/test_hol_content_parser.xml", "rb") as f:
            xml = f.read()
            result = DmsContentParserHol(xml)

            self.assertEqual(result.format, "HOL")
            self.assertEqual(result.user, "ROOT")
            self.assertEqual(result.version, "11.10.773.24186")
            self.assertEqual(result.timestamp, datetime(2025, 8, 15, 15, 15, 37))
            self.assertEqual(result.instance, "tests.test_ecm_dms_query_parser")
            self.assertEqual(result.station, "ecm4")
            self.assertEqual(result.output_language, 0)

            obj_types = list(result.object_types())

            # check document type
            ot = obj_types[0]
            self.assertEqual(ot.archive.id, 1)
            self.assertEqual(ot.archive.name, "Unittest DMS")
            self.assertEqual(ot.archive.osguid, "90F077D2CC264BA194997584DC423B23")
            self.assertEqual(ot.name, "Dokument")
            self.assertEqual(ot.cotype, 1)
            self.assertEqual(ot.internal_name, "Unittest_DMS_Document")
            self.assertEqual(ot.id, 262145)
            self.assertEqual(ot.modul, "MULTIDOC")
            self.assertEqual(ot.osguid, "8A6E3E8CDE6D4F4B90D05399E032EB20")
            self.assertEqual(ot.type, "DOCUMENT")
            self.assertEqual(ot.table, "object2")

            objects = list(ot.objects())

            # Object 0
            obj = objects[0]

            self.assertEqual(obj.id, 352)

            self.assertEqual(obj.file_properties.count, 1)
            self.assertEqual(obj.file_properties.documentpagecount, 383)
            self.assertEqual(obj.file_properties.extension, "pdf")
            self.assertEqual(obj.file_properties.iconid, 1073743982)
            self.assertEqual(obj.file_properties.mimetype, "application/pdf")
            self.assertEqual(obj.file_properties.mimetypegroup, "PDF")
            self.assertEqual(obj.file_properties.size, 2788420)

            base_params = obj.base_params
            self.assertIsNotNone(base_params)
            if base_params is not None:
                self.assertEqual(base_params.created, datetime(2025, 8, 15, 14, 47, 56))
                self.assertEqual(base_params.creator, "ROOT")
                self.assertEqual(base_params.foreign_id, "0")
                self.assertEqual(base_params.modified, datetime(2025, 8, 15, 14, 47, 57))
                self.assertEqual(base_params.modifier, "ROOT")
                self.assertEqual(base_params.links, 0)
                self.assertEqual(base_params.owner, "ROOT")
                self.assertEqual(base_params.owner_osguid, "35100CD4D441420B90811DC90766D64F")
                self.assertEqual(base_params.system_id, 0)
                self.assertEqual(base_params.pdf_annotation_count, 0)
                self.assertEqual(base_params.txt_notice_count, 0)

            self.assertEqual(obj.fields["Name"].internal_name, "Name")
            self.assertEqual(obj.fields["Name"].dbname, "feld1")
            self.assertEqual(obj.fields["Name"].display_value, "Server-API")
            self.assertEqual(obj.fields["Name"].name, "Name")
            self.assertEqual(obj.fields["Name"].osguid, "FE49BE50330C47E6BE385A33513CB547")
            self.assertEqual(obj.fields["Name"].ostype, "X")
            self.assertEqual(obj.fields["Name"].size, 255)
            self.assertEqual(obj.fields["Name"].system, False)
            self.assertEqual(obj.fields["Name"].value(), "Server-API")
            self.assertEqual(obj.fields["Name"].typed_value(str), "Server-API")

            self.assertEqual(obj.fields["DateField"].internal_name, "DateField")
            self.assertEqual(obj.fields["DateField"].dbname, "datum1")
            self.assertEqual(obj.fields["DateField"].display_value, "15.08.2025")
            self.assertEqual(obj.fields["DateField"].name, "Date Field")
            self.assertEqual(obj.fields["DateField"].osguid, "55550F928F67497D8EA443D3802BA6F6")
            self.assertEqual(obj.fields["DateField"].ostype, "D")
            self.assertEqual(obj.fields["DateField"].size, 10)
            self.assertEqual(obj.fields["DateField"].system, False)
            self.assertEqual(obj.fields["DateField"].value(), datetime(2025, 8, 15, 0, 0))
            self.assertEqual(obj.fields["DateField"].typed_value(datetime), datetime(2025, 8, 15, 0, 0))

            self.assertEqual(obj.fields["DateTimeField"].internal_name, "DateTimeField")
            self.assertEqual(obj.fields["DateTimeField"].dbname, "zahl1")
            self.assertEqual(obj.fields["DateTimeField"].display_value, "15.08.2025 14:45:18")
            self.assertEqual(obj.fields["DateTimeField"].name, "DateTime Field")
            self.assertEqual(obj.fields["DateTimeField"].osguid, "FFD713B64D4C44AF9F7D9981CECFBA52")
            self.assertEqual(obj.fields["DateTimeField"].ostype, "9")
            self.assertEqual(obj.fields["DateTimeField"].size, 19)
            self.assertEqual(obj.fields["DateTimeField"].system, False)
            self.assertEqual(obj.fields["DateTimeField"].value(), datetime(2025, 8, 15, 14, 45, 18))
            self.assertEqual(obj.fields["DateTimeField"].typed_value(datetime), datetime(2025, 8, 15, 14, 45, 18))

            self.assertEqual(obj.fields["DoubleField"].internal_name, "DoubleField")
            self.assertEqual(obj.fields["DoubleField"].dbname, "real1")
            self.assertEqual(obj.fields["DoubleField"].display_value, "3.23")
            self.assertEqual(obj.fields["DoubleField"].name, "Double Field")
            self.assertEqual(obj.fields["DoubleField"].osguid, "80AFE59C1BF5482092AF4E2EF1EFF294")
            self.assertEqual(obj.fields["DoubleField"].ostype, "#")
            self.assertEqual(obj.fields["DoubleField"].size, 15)
            self.assertEqual(obj.fields["DoubleField"].system, False)
            self.assertEqual(obj.fields["DoubleField"].value(), 3.23)
            self.assertEqual(obj.fields["DoubleField"].typed_value(float), 3.23)

            self.assertEqual(obj.fields["DoubleField"].internal_name, "DoubleField")
            self.assertEqual(obj.fields["DoubleField"].dbname, "real1")
            self.assertEqual(obj.fields["DoubleField"].display_value, "3.23")
            self.assertEqual(obj.fields["DoubleField"].name, "Double Field")
            self.assertEqual(obj.fields["DoubleField"].osguid, "80AFE59C1BF5482092AF4E2EF1EFF294")
            self.assertEqual(obj.fields["DoubleField"].ostype, "#")
            self.assertEqual(obj.fields["DoubleField"].size, 15)
            self.assertEqual(obj.fields["DoubleField"].system, False)
            self.assertEqual(obj.fields["DoubleField"].value(), 3.23)
            self.assertEqual(obj.fields["DoubleField"].typed_value(float), 3.23)

            self.assertEqual(obj.fields["CheckboxField"].internal_name, "CheckboxField")
            self.assertEqual(obj.fields["CheckboxField"].dbname, "zahl2")
            self.assertEqual(obj.fields["CheckboxField"].display_value, "1")
            self.assertEqual(obj.fields["CheckboxField"].name, "Checkbox Field")
            self.assertEqual(obj.fields["CheckboxField"].osguid, "42E10B0DE1444297BF026FCCC9EA56A2")
            self.assertEqual(obj.fields["CheckboxField"].ostype, "0")
            self.assertEqual(obj.fields["CheckboxField"].size, 1)
            self.assertEqual(obj.fields["CheckboxField"].system, False)
            self.assertEqual(obj.fields["CheckboxField"].value(), 1)
            self.assertEqual(obj.fields["CheckboxField"].typed_value(int), 1)

            self.assertEqual(obj.fields["RadioField1"].internal_name, "RadioField1")
            self.assertEqual(obj.fields["RadioField1"].dbname, "zahl3")
            self.assertEqual(obj.fields["RadioField1"].display_value, "Radio Field 1")
            self.assertEqual(obj.fields["RadioField1"].name, "Radio Field 1")
            self.assertEqual(obj.fields["RadioField1"].osguid, "AE0575B6FE074E3FB9712CD58E16FDF3")
            self.assertEqual(obj.fields["RadioField1"].ostype, "1")
            self.assertEqual(obj.fields["RadioField1"].size, 3)
            self.assertEqual(obj.fields["RadioField1"].system, False)
            self.assertEqual(obj.fields["RadioField1"].value(), 0)
            self.assertEqual(obj.fields["RadioField1"].typed_value(int), 0)

            self.assertEqual(obj.fields["TimeField"].internal_name, "TimeField")
            self.assertEqual(obj.fields["TimeField"].dbname, "zahl6")
            self.assertEqual(obj.fields["TimeField"].display_value, "14:45:18")
            self.assertEqual(obj.fields["TimeField"].name, "Time Field")
            self.assertEqual(obj.fields["TimeField"].osguid, "701C64F465314642A72AF29B5532BA07")
            self.assertEqual(obj.fields["TimeField"].ostype, "9")
            self.assertEqual(obj.fields["TimeField"].size, 8)
            self.assertEqual(obj.fields["TimeField"].system, False)
            self.assertEqual(obj.fields["TimeField"].value(), time(14, 45, 18))
            self.assertEqual(obj.fields["TimeField"].typed_value(time), time(14, 45, 18))

            self.assertEqual(obj.table_fields["TableField"].dbname, "list1")
            self.assertEqual(obj.table_fields["TableField"].internal_name, "TableField")
            self.assertEqual(obj.table_fields["TableField"].name, "Table Field")
            self.assertEqual(obj.table_fields["TableField"].osguid, "9ABBD7483C06436E90BA7756F57A88C3")
            self.assertEqual(obj.table_fields["TableField"].ostype, "W")

            rows = obj.table_fields["TableField"].rows
            row = rows[0]

            self.assertEqual(row["TableCol1"].dbname, "feld1")
            self.assertEqual(row["TableCol1"].display_value, "Test Table")
            self.assertEqual(row["TableCol1"].name, "Table Col 1")
            self.assertEqual(row["TableCol1"].internal_name, "TableCol1")
            self.assertEqual(row["TableCol1"].osguid, "05347385AB18488790CB231A3816DA84")
            self.assertEqual(row["TableCol1"].ostype, "X")
            self.assertEqual(row["TableCol1"].row_id, 1)
            self.assertEqual(row["TableCol1"].size, 50)

            self.assertEqual(row["TableCol2"].dbname, "zahl1")
            self.assertEqual(row["TableCol2"].display_value, "42")
            self.assertEqual(row["TableCol2"].name, "Table Col 2")
            self.assertEqual(row["TableCol2"].internal_name, "TableCol2")
            self.assertEqual(row["TableCol2"].osguid, "A8311BDB9DE74CFB8F850793184BAA19")
            self.assertEqual(row["TableCol2"].ostype, "9")
            self.assertEqual(row["TableCol2"].row_id, 1)
            self.assertEqual(row["TableCol2"].size, 8)

            self.assertEqual(row["TableCol3"].dbname, "datum1")
            self.assertEqual(row["TableCol3"].display_value, "15.08.2025")
            self.assertEqual(row["TableCol3"].name, "Table Col 3")
            self.assertEqual(row["TableCol3"].internal_name, "TableCol3")
            self.assertEqual(row["TableCol3"].osguid, "025AA57EA69B4BCCA3EECE9E96330DED")
            self.assertEqual(row["TableCol3"].ostype, "D")
            self.assertEqual(row["TableCol3"].row_id, 1)
            self.assertEqual(row["TableCol3"].size, 10)

            self.assertEqual(row["TableCol4"].dbname, "real1")
            self.assertEqual(row["TableCol4"].display_value, "3.48")
            self.assertEqual(row["TableCol4"].name, "Table Col 4")
            self.assertEqual(row["TableCol4"].internal_name, "TableCol4")
            self.assertEqual(row["TableCol4"].osguid, "DFE11C1B9C7A42628CA2539060ECFDBC")
            self.assertEqual(row["TableCol4"].ostype, "#")
            self.assertEqual(row["TableCol4"].row_id, 1)
            self.assertEqual(row["TableCol4"].size, 10)

    def test_lol_query_parser(self):
        with open("tests/testdata/test_lol_content_parser.xml", "rb") as f:
            xml = f.read()
            result = DmsContentParserLol(xml)

            self.assertEqual(result.format, "LOL")
            self.assertEqual(result.user, "ROOT")
            self.assertEqual(result.version, "11.10.773.24186")
            self.assertEqual(result.timestamp, datetime(2025, 8, 18, 8, 57, 52))
            self.assertEqual(result.instance, "tests.test_ecm_dms_query_builder")
            self.assertEqual(result.station, "ecm4")
            self.assertEqual(result.output_language, 0)

            obj_types = list(result.object_types())

            # check document type
            ot = obj_types[0]
            self.assertEqual(ot.archive.id, 1)
            self.assertEqual(ot.archive.name, "Unittest DMS")
            self.assertEqual(ot.archive.osguid, "90F077D2CC264BA194997584DC423B23")
            self.assertEqual(ot.name, "Dokument")
            self.assertEqual(ot.cotype, 1)
            self.assertEqual(ot.internal_name, "Unittest_DMS_Document")
            self.assertEqual(ot.id, 262145)
            self.assertEqual(ot.modul, "MULTIDOC")
            self.assertEqual(ot.osguid, "8A6E3E8CDE6D4F4B90D05399E032EB20")
            self.assertEqual(ot.type, "DOCUMENT")
            self.assertEqual(ot.table, "object2")

            rows = list(ot.rows())

            # Object 0
            row = rows[0]

            self.assertEqual(row.id, 352)

            self.assertEqual(row.fields["OBJECT_COUNT"].value(), 1)
            self.assertEqual(row.fields["OBJECT_DOCPAGECOUNT"].value(), 383)
            self.assertEqual(row.fields["OBJECT_MIMETYPEID"].value(), 18)
            self.assertEqual(row.fields["OBJECT_FILESIZE"].value(), 2788420)

            self.assertEqual(row.fields["OBJECT_TIME"].value(), datetime(2025, 8, 15, 14, 47, 56))
            self.assertEqual(row.fields["OBJECT_CRID"].value(), "ROOT")
            self.assertEqual(row.fields["OBJECT_FOREIGNID"].value(), "0")
            self.assertEqual(row.fields["OBJECT_MODIFYTIME"].value(), datetime(2025, 8, 15, 14, 47, 57))
            self.assertEqual(row.fields["OBJECT_MODIFYUSER"].value(), "ROOT")
            self.assertEqual(row.fields["OBJECT_LINKS"].value(), 0)
            self.assertEqual(row.fields["OBJECT_USERGUID"].display_value, "ROOT")
            self.assertEqual(row.fields["OBJECT_USERGUID"].value(), "35100CD4D441420B90811DC90766D64F")
            self.assertEqual(row.fields["OBJECT_SYSTEMID"].value(), None)
            self.assertEqual(row.fields["OBJECT_PDFANNOTATIONCOUNT"].value(), 0)
            self.assertEqual(row.fields["OBJECT_TXTNOTICECOUNT"].value(), 0)

            self.assertEqual(row.fields["Name"].internal_name, "Name")
            self.assertEqual(row.fields["Name"].dbname, "feld1")
            self.assertEqual(row.fields["Name"].display_value, "Server-API")
            self.assertEqual(row.fields["Name"].name, "Name")
            self.assertEqual(row.fields["Name"].osguid, "FE49BE50330C47E6BE385A33513CB547")
            self.assertEqual(row.fields["Name"].ostype, "X")
            self.assertEqual(row.fields["Name"].size, 255)
            self.assertEqual(row.fields["Name"].system, False)
            self.assertEqual(row.fields["Name"].value(), "Server-API")
            self.assertEqual(row.fields["Name"].typed_value(str), "Server-API")

            self.assertEqual(row.fields["DateField"].internal_name, "DateField")
            self.assertEqual(row.fields["DateField"].dbname, "datum1")
            self.assertEqual(row.fields["DateField"].display_value, "15.08.2025")
            self.assertEqual(row.fields["DateField"].name, "Date Field")
            self.assertEqual(row.fields["DateField"].osguid, "55550F928F67497D8EA443D3802BA6F6")
            self.assertEqual(row.fields["DateField"].ostype, "D")
            self.assertEqual(row.fields["DateField"].size, 10)
            self.assertEqual(row.fields["DateField"].system, False)
            self.assertEqual(row.fields["DateField"].value(), datetime(2025, 8, 15, 0, 0))
            self.assertEqual(row.fields["DateField"].typed_value(datetime), datetime(2025, 8, 15, 0, 0))

            self.assertEqual(row.fields["DateTimeField"].internal_name, "DateTimeField")
            self.assertEqual(row.fields["DateTimeField"].dbname, "zahl1")
            self.assertEqual(row.fields["DateTimeField"].display_value, "15.08.2025 14:45:18")
            self.assertEqual(row.fields["DateTimeField"].name, "DateTime Field")
            self.assertEqual(row.fields["DateTimeField"].osguid, "FFD713B64D4C44AF9F7D9981CECFBA52")
            self.assertEqual(row.fields["DateTimeField"].ostype, "9")
            self.assertEqual(row.fields["DateTimeField"].size, 19)
            self.assertEqual(row.fields["DateTimeField"].system, False)
            self.assertEqual(row.fields["DateTimeField"].value(), datetime(2025, 8, 15, 14, 45, 18))
            self.assertEqual(row.fields["DateTimeField"].typed_value(datetime), datetime(2025, 8, 15, 14, 45, 18))

            self.assertEqual(row.fields["DoubleField"].internal_name, "DoubleField")
            self.assertEqual(row.fields["DoubleField"].dbname, "real1")
            self.assertEqual(row.fields["DoubleField"].display_value, "3.23")
            self.assertEqual(row.fields["DoubleField"].name, "Double Field")
            self.assertEqual(row.fields["DoubleField"].osguid, "80AFE59C1BF5482092AF4E2EF1EFF294")
            self.assertEqual(row.fields["DoubleField"].ostype, "#")
            self.assertEqual(row.fields["DoubleField"].size, 15)
            self.assertEqual(row.fields["DoubleField"].system, False)
            self.assertEqual(row.fields["DoubleField"].value(), 3.23)
            self.assertEqual(row.fields["DoubleField"].typed_value(float), 3.23)

            self.assertEqual(row.fields["DoubleField"].internal_name, "DoubleField")
            self.assertEqual(row.fields["DoubleField"].dbname, "real1")
            self.assertEqual(row.fields["DoubleField"].display_value, "3.23")
            self.assertEqual(row.fields["DoubleField"].name, "Double Field")
            self.assertEqual(row.fields["DoubleField"].osguid, "80AFE59C1BF5482092AF4E2EF1EFF294")
            self.assertEqual(row.fields["DoubleField"].ostype, "#")
            self.assertEqual(row.fields["DoubleField"].size, 15)
            self.assertEqual(row.fields["DoubleField"].system, False)
            self.assertEqual(row.fields["DoubleField"].value(), 3.23)
            self.assertEqual(row.fields["DoubleField"].typed_value(float), 3.23)

            self.assertEqual(row.fields["CheckboxField"].internal_name, "CheckboxField")
            self.assertEqual(row.fields["CheckboxField"].dbname, "zahl2")
            self.assertEqual(row.fields["CheckboxField"].display_value, "1")
            self.assertEqual(row.fields["CheckboxField"].name, "Checkbox Field")
            self.assertEqual(row.fields["CheckboxField"].osguid, "42E10B0DE1444297BF026FCCC9EA56A2")
            self.assertEqual(row.fields["CheckboxField"].ostype, "0")
            self.assertEqual(row.fields["CheckboxField"].size, 1)
            self.assertEqual(row.fields["CheckboxField"].system, False)
            self.assertEqual(row.fields["CheckboxField"].value(), 1)
            self.assertEqual(row.fields["CheckboxField"].typed_value(int), 1)

            self.assertEqual(row.fields["RadioField1"].internal_name, "RadioField1")
            self.assertEqual(row.fields["RadioField1"].dbname, "zahl3")
            self.assertEqual(row.fields["RadioField1"].display_value, "Radio Field 1")
            self.assertEqual(row.fields["RadioField1"].name, "Radio Field 1")
            self.assertEqual(row.fields["RadioField1"].osguid, "AE0575B6FE074E3FB9712CD58E16FDF3")
            self.assertEqual(row.fields["RadioField1"].ostype, "1")
            self.assertEqual(row.fields["RadioField1"].size, 3)
            self.assertEqual(row.fields["RadioField1"].system, False)
            self.assertEqual(row.fields["RadioField1"].value(), 0)
            self.assertEqual(row.fields["RadioField1"].typed_value(int), 0)

            self.assertEqual(row.fields["TimeField"].internal_name, "TimeField")
            self.assertEqual(row.fields["TimeField"].dbname, "zahl6")
            self.assertEqual(row.fields["TimeField"].display_value, "14:45:18")
            self.assertEqual(row.fields["TimeField"].name, "Time Field")
            self.assertEqual(row.fields["TimeField"].osguid, "701C64F465314642A72AF29B5532BA07")
            self.assertEqual(row.fields["TimeField"].ostype, "9")
            self.assertEqual(row.fields["TimeField"].size, 8)
            self.assertEqual(row.fields["TimeField"].system, False)
            self.assertEqual(row.fields["TimeField"].value(), time(14, 45, 18))
            self.assertEqual(row.fields["TimeField"].typed_value(time), time(14, 45, 18))


if __name__ == "__main__":
    unittest.main()
