import unittest

from ecmind_blue_client.ecm.builder import (
    DmsQueryBuilderHol,
    DmsQueryLinkedValue,
    DmsQueryParamValue,
)
from ecmind_blue_client.ecm.builder._dms_query_builder_lol import DmsQueryBuilderLol
from ecmind_blue_client.ecm.parser import DmsContentParserHol
from ecmind_blue_client.ecm.parser._dms_content_parser_lol import DmsContentParserLol
from ecmind_blue_client.pool import SyncPoolClient
from ecmind_blue_client.rpc import Jobs
from tests._test_settings import ECM_SERVERS, ECM_USERNAME, ECM_PASSWORD


class TestDmsQueryBuilder(unittest.TestCase):
    def setUp(self):
        self.client = SyncPoolClient(servers=ECM_SERVERS, username=ECM_USERNAME, password=ECM_PASSWORD)

    def test_simple_job_in_context(self):
        b = DmsQueryBuilderHol()
        b.param("TableValue", "TestTableValue1")

        with b.object_type("UnittestDoc") as ot:
            with ot.fields() as f:
                f.field("OSID", 1, "ASC")
                f.field("StringField")
                f.field("DateField")
                f.field("DateTimeField")
                f.field("DoubleField")
                f.field("CheckboxField")
                f.field("RadioField1")
                f.field("RadioField2")
                f.field("TableField")

            with ot.condition_object() as co:
                with co.table_condition("TableField") as tc:
                    tc.table_column("TableCol1", DmsQueryParamValue("TableValue"))

            with ot.condition_object("Unittest") as co:
                co.field_condition("StringField", "Unittest Folder")

        with b.object_type("UnittestReg2") as ot:
            with ot.condition_object() as co:
                co.field_condition("StringField", "Roland Koller")

        xml = b.to_xml()
        print(xml.decode("utf-8"))

        result = self.client.execute(Jobs.DMS_GETRESULTLIST, Flags=16, XML=xml)

        print(result.files[0].bytes().decode("UTF-16"))

        p = DmsContentParserHol(result.files[0])
        for ot in p.object_types():
            print(ot.statistics.total_hits)
            for obj in ot.objects():
                if "DoubleField" in obj.fields:
                    data = obj.fields["DoubleField"].typed_value(float)
                    if data is not None:
                        pass
                if "TableField" in obj.table_fields:
                    for row in obj.table_fields["TableField"].rows:
                        for column in row.values():
                            print(column.internal_name)

    def test_simple_fulltext(self):

        b = DmsQueryBuilderHol()
        with b.object_type("UnitTest") as ot:
            ot.fields("ALL")
            with ot.condition_object() as co:
                co.fulltext("test")
        xml = b.to_xml()
        print(xml.decode("utf-8"))
        result = self.client.execute(Jobs.DMS_GETRESULTLIST, Flags=16, XML=xml)

        p = DmsContentParserHol(result.files[0])

        for ot in p.object_types():
            print(ot.statistics.total_hits)
            for obj in ot.objects():
                print(obj.id)

    def test_simple_external_objects(self):
        b = DmsQueryBuilderHol()
        b.pagesize = 5
        with b.object_type("UnitTestDoc") as ot:
            with ot.fields("MIN") as fields:
                fields.field("DateField", link_name="datum")
            with ot.external_object_type("UnitTest") as co:
                co.fields("ALL")
                co.field_condition("DateField", DmsQueryLinkedValue("datum"))

        xml = b.to_xml()
        print(xml.decode("utf-8"))
        result = self.client.execute(Jobs.DMS_GETRESULTLIST, Flags=16, XML=xml)

        print(result.files[0].bytes().decode("utf-16"))

        p = DmsContentParserHol(result.files[0])
        for ot in p.object_types():
            print(ot.statistics.total_hits)
            for obj in ot.objects():
                print(obj.id)

    def test_lol(self):
        b = DmsQueryBuilderLol()
        with b.object_type("UnitTestDoc") as ot:
            with ot.fields("MIN") as fields:
                fields.field("DateField", link_name="datum")

        xml = b.to_xml()
        print(xml.decode("utf-8"))
        result = self.client.execute(Jobs.DMS_GETRESULTLIST, Flags=16, XML=xml)

        print(result.files[0].bytes().decode("utf-16"))

        p = DmsContentParserLol(result.files[0])
        for ot in p.object_types():
            print(ot.statistics.total_hits)
            for row in ot.rows():
                print(row.id)


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