from datetime import datetime
import classifier
import graphql
import xml.etree.ElementTree as ET


class CatalogRequestInfo:
    is_full = False
    user_id = 0


class Catalog:
    CD_SERVER_COLLECTIONS_NAME = "CD-Server Collections Set"
    CD_SERVER_COLLECTIONS_VERSION = "1.0"
    CD_SERVER_COLLECTION_TYPE = "ISSUE"
    COLLECTIONS_TAG_NAME = "COLLECTIONS"
    NAME_ATTRIBUTE_NAME = "Name"
    VERSION_ATTRIBUTE_NAME = "Version"
    CREATED_ATTRIBUTE_NAME = "Created"
    FULL_ATTRIBUTE_NAME = "Full"
    COLLECTION_TAG_NAME = "COLLECTION"
    TYPE_ATTRIBUTE_NAME = "Type"
    CHARTS_TAG_NAME = "CHARTS"
    CHART_TAG_NAME = "CHART"
    METADATA_VERSION_ATTRIBUTE_NAME = "MetadataVersion"
    ID_ATTRIBUTE_NAME = "ID"
    DELETED_ATTRIBUTE_NAME = "Deleted"
    CLOSED_ATTRIBUTE_NAME = "Closed"
    FOLDERS_TAG_NAME = "FOLDERS"
    FOLDER_TAG_NAME = "FOLDER"
    REF_TAG_NAME = "REF"
    CHART_ATTRIBUTE_NAME = "Chart"

    COLLECTION_NAME = 'CD-Server Collection'

    request_info: CatalogRequestInfo

    def write_catalog(self):
        date = datetime.now()

        collections = ET.Element(self.COLLECTIONS_TAG_NAME, {
            self.NAME_ATTRIBUTE_NAME: self.COLLECTION_NAME,
            self.VERSION_ATTRIBUTE_NAME: self.CD_SERVER_COLLECTIONS_VERSION,
            self.CREATED_ATTRIBUTE_NAME: self.date_to_string(date),
        })
        if self.request_info.is_full:
            collections.set(self.FULL_ATTRIBUTE_NAME, 'true')
        if classifier.CLASSIFIER_VERSION:
            collections.set(self.METADATA_VERSION_ATTRIBUTE_NAME, classifier.CLASSIFIER_VERSION)

        self.write_collections(collections, date)

        return ET.tostring(collections, encoding='unicode', xml_declaration=True)

    def date_to_string(self, date):
        return str(date.timestamp())

    def write_collections(self, collections, date):
        collection = ET.SubElement(collections, self.COLLECTION_TAG_NAME, {
            self.NAME_ATTRIBUTE_NAME: self.COLLECTION_NAME,
            self.CREATED_ATTRIBUTE_NAME: self.date_to_string(date),
            self.TYPE_ATTRIBUTE_NAME: self.CD_SERVER_COLLECTION_TYPE,
        })
        self.write_charts(collection)

    def write_charts(self, collection):
        charts = ET.SubElement(collection, self.CHARTS_TAG_NAME)
        for c in []:
            self.write_chart(charts, c)

    def write_chart(self, charts, chart):
        ch = ET.SubElement(charts, self.CHART_TAG_NAME)
