from django.core.management.base import BaseCommand, CommandError
from sde.models import *
import yaml


class Command(BaseCommand):
    help = 'Import SDE data from YAML files'

    def add_arguments(self, parser):
        parser.add_argument('path', type=str, help='Path to the SDE YAML files')

    def handle(self, *args, **options):
        # Import the SDE data
        self.stdout.write(self.style.SUCCESS('Importing SDE data'))
        self.import_sde_data(path=options['path'])
        self.stdout.write(self.style.SUCCESS('Successfully imported SDE data'))

    def import_sde_data(self, path):
        self.import_icons(path + "/iconIDs.yaml")
        self.import_categories(path + "/categoryIDs.yaml")
        self.import_groups(path + "/groupIDs.yaml")
        self.import_marketgroups(path + "/marketGroups.yaml")
        self.import_metagroups(path + "/metaGroups.yaml")
        self.import_types(path + "/typeIDs.yaml")
        self.import_type_materials(path + "/typeMaterials.yaml")

    def import_icons(self, path):
        with open(path) as file:
            icons = yaml.load(file, Loader=yaml.FullLoader)
            for id, icon in icons.items():
                SDEIcon.objects.update_or_create(
                    id=id,
                    defaults={
                        'iconFile': icon['iconFile'],
                        'description': icon['description'] if "description" in icon else ""
                    }
                )
        self.stdout.write(self.style.SUCCESS('Icons imported'))

    def import_categories(self, path):
        with open(path) as file:
            categories = yaml.load(file, Loader=yaml.FullLoader)
            for id, category in categories.items():
                SDECategory.objects.update_or_create(
                    id=id,
                    defaults={
                        'icon': SDEIcon.objects.get(id=category['iconID']) if "iconID" in category else None,
                        'name': category['name']['en'],
                        'published': category['published']
                    }
                )
        self.stdout.write(self.style.SUCCESS('Categories imported'))

    def import_groups(self, path):
        with open(path) as file:
            groups = yaml.load(file, Loader=yaml.FullLoader)
            for id, group in groups.items():
                SDEGroup.objects.update_or_create(
                    id=id,
                    defaults={
                        'category': SDECategory.objects.get(id=group['categoryID']),
                        'name': group['name'],
                        'published': group['published'],
                        'useBasePrice': group['useBasePrice'],
                        'fittableNonSingleton': group['fittableNonSingleton'],
                        'anchored': group['anchored'],
                        'anchorable': group['anchorable'],
                        'icon': SDEIcon.objects.get(id=group['iconID']) if "iconID" in group else None
                    }
                )
        self.stdout.write(self.style.SUCCESS('Groups imported'))

    def import_marketgroups(self, path):
        with open(path) as file:
            marketgroups = yaml.load(file, Loader=yaml.FullLoader)
            for id, marketgroup in marketgroups.items():
                SDEMarektGroup.objects.update_or_create(
                    id=id,
                    defaults={
                        'icon': SDEIcon.objects.get(id=marketgroup['iconID']) if "iconID" in marketgroup else None,
                        'name': marketgroup['nameID']['en'],
                        'description': marketgroup['descriptionID']['en'] if "descriptionID" in marketgroup else "",
                        'hasTypes': marketgroup['hasTypes'],
                    }
                )
            self.stdout.write(self.style.SUCCESS('Marketgroups imported'))
            for id, marketgroup in marketgroups.items():
                if "parentGroupID" in marketgroup:
                    sde_mg = SDEMarektGroup.objects.get(id=id)
                    sde_mg.parent_marketgroup = SDEMarektGroup.objects.get(id=marketgroup['parentGroupID'])
                    sde_mg.save()
        self.stdout.write(self.style.SUCCESS('Marketgroups linked'))

    def import_metagroups(self, path):
        with open(path) as file:
            metagroups = yaml.load(file, Loader=yaml.FullLoader)
            for id, metagroup in metagroups.items():
                SDEMetaGroup.objects.update_or_create(
                    id=id,
                    defaults={
                        'icon': SDEIcon.objects.get(id=metagroup['iconID']) if "iconID" in metagroup else None,
                        'name': metagroup['nameID']['en'],
                        'iconSuffix': metagroup['iconSuffix'] if "iconSuffix" in metagroup else "",
                    }
                )
        self.stdout.write(self.style.SUCCESS('Metagroups imported'))

    def import_types(self, path):
        with open(path) as file:
            types = yaml.load(file, Loader=yaml.FullLoader)
            for id, type in types.items():
                SDEType.objects.update_or_create(
                    id=id,
                    defaults={
                        'group': SDEGroup.objects.get(id=type['groupID']),
                        'marketgroup': SDEMarektGroup.objects.get(id=type['marketGroupID']) if "marketGroupID" in type else None,
                        'metagroup': SDEMetaGroup.objects.get(id=type['metaGroupID']) if "metaGroupID" in type else None,
                        'name': type['name']['en'],
                        'description': type['description']['en'] if "description" in type else "",
                        'published': type['published'],
                        'basePrice': type['basePrice'] if "basePrice" in type else 0,
                        'icon': SDEIcon.objects.get(id=type['iconID']) if "iconID" in type else None,
                        'volume': type['volume'] if "volume" in type else 0,
                        'portionSize': type['portionSize'],
                    }
                )
        self.stdout.write(self.style.SUCCESS('Types imported'))

    def import_type_materials(self, path):
        with open(path) as file:
            type_materials = yaml.load(file, Loader=yaml.FullLoader)
            for id, type_material in type_materials.items():
                for material in type_material['materials']:
                    SDETypeMaterial.objects.update_or_create(
                        type=SDEType.objects.get(id=id),
                        material_type=SDEType.objects.get(id=material['materialTypeID']),
                        defaults={
                            'quantity': material['quantity'],
                        }
                    )
        self.stdout.write(self.style.SUCCESS('Materials imported'))
