from datetime import datetime, timedelta
from enumerations import ReplicationPackageStatusEnum
from models import CorrectingReplicationOutPackage, ReplicantInfo
from typing import List
import xml.etree.ElementTree as ET


class CorrectingReplicationService:
    EVENT_TYPE: str = 'correcting_replication'
    object_list_size: int = 0
    deleted_objects_count: int = 0
    PACKAGE_TAG_NAME: str = 'package'
    PACKAGE_ID_ATTRIBUTE_NAME: str = 'pkg_id'
    SERVER_VERSION_ATTRIBUTE_NAME: str = 'server_version'
    SERVER_ID_ATTRIBUTE_NAME: str = 'server_id'
    OBJECTS_TAG_NAME: str = 'objects'
    OBJECT_TAG_NAME: str = 'object'
    OBJECT_ID_ATTRIBUTE_NAME: str = 'id'
    IS_RSC_OBJECT: str = 'is_rsc'
    OBJECT_SOURCE_ATTRIBUTE_NAME: str = 'source'
    OBJECT_VERSION_ATTRIBUTE_NAME: str = 'version'
    METADATA_VERSION_ATTRIBUTE_NAME: str = 'md_version'
    ACTUAL_VERSION_DATE_ATTRIBUTE_NAME: str = 'version_date'
    OBJECT_MISSING_ATTRIBUTE_NAME: str = 'missing'
    CLASSIFIER_VERSION_ATTRIBUTE_NAME: str = 'metadata_clientVersion'
    RESULT_TAG_NAME: str = 'result'
    RESULT_MESSAGE_ATTRIBUTE_NAME: str = 'result_message'
    VERSION_ID_ATTRIBUTE_NAME: str = 'version_id'
    VERSION_SOURCE_ATTRIBUTE_NAME: str = 'version_source'

    def start_correcting_replication(self, replicant_id: str, replication_period: int, replication_timeout: int,
                                     manual_run: bool):
        replicant = ReplicantInfo()
        self.start_correcting_replication(replicant, replication_period, replication_timeout, manual_run)

    def start_correcting_replication(self, except_replicant_id: List[str], replication_period: int,
                                     replication_timeout: int, manual_run: bool):
        pass

    def start_correcting_replication(self, replicant: ReplicantInfo, replication_period: int, replication_timeout: int,
                                     manual_run: bool):
        pass

    def create_replication_package(self, replicant: ReplicantInfo, replication_period: int, replication_timeout: int) -> CorrectingReplicationOutPackage:
        pkg = CorrectingReplicationOutPackage()
        pkg.filter_string = replicant.filter_string
        pkg.replicant_id = replicant.replicant_id
        pkg.server_address = replicant.server_address
        pkg.replication_period = replication_period
        pkg.replication_timeout = replication_timeout
        pkg.status = ReplicationPackageStatusEnum.SENT_REQUEST_TO_CHILD
        date = datetime.now()
        pkg.replication_date = date
        if replication_period:
            delta = timedelta(days=-replication_period)
            pkg.from_date = date + delta
        if replication_timeout:
            delta = timedelta(seconds=replication_timeout)
            pkg.replication_timeout_date = date + delta
        return pkg

    def get_package_xml(self, pkg: CorrectingReplicationOutPackage, request_uuid: str) -> str:
        request_xml: ET.Element = xml_service.get_common_request_xml(request_uuid)
        element = ET.SubElement(request_xml, self.PACKAGE_TAG_NAME, {
            self.PACKAGE_ID_ATTRIBUTE_NAME: pkg.package_id,
            self.SERVER_VERSION_ATTRIBUTE_NAME: '1.0.0',
            self.SERVER_ID_ATTRIBUTE_NAME: 'ipd-server',
            self.CLASSIFIER_VERSION_ATTRIBUTE_NAME: classifier.,
        })
        xml_service.set_request_uuid(request_xml, request_uuid)
        return ET.tostring(request_xml, encoding='unicode', xml_declaration=True)
