import os
from config.settings.base import MEDIA_ROOT
from countries.models import Country, Flag
import json
# from django.utils.text import slugify

COUNTRY_FILE = 'result.json'


def create_flag(country):
    print(f'Inside create_flag func with: {country.name}')
    flag, created = Flag.objects.get_or_create(
        country=country,
        title=f'Флаг {country.ru_name_rod}'
        )
    # print(f'get_or_create: {flag} - {created}')
    # flag.title = f'Флаг {country.ru_name_rod}'
    # flag.slug = slugify(flag.title)
    # flag.save()
    print(f'All is ok?: {flag.title}')


def create_countries():
    print('+[Success] Start importing countries:')
    with open(os.path.join(MEDIA_ROOT, COUNTRY_FILE), "r") as f:
        country_json = json.load(f)
        print(f'+[Success] File {COUNTRY_FILE} successfully opened!')
    # Get data from json
    for code, data in country_json.items():
        print(f'+[Success] Code {code} in loop')
        iso_code_a2 = data['iso_2']
        # if iso_code_a2 == 'AE':
        if iso_code_a2:
            print(f'+[Success] Enter into {iso_code_a2} condition')
            country, created = Country.objects.get_or_create(iso_code_a2=iso_code_a2)

            print(f'get_or_create {country} is {created}')

            country.name = data['name']['ru_short_form']
            country.conventional_long_name = data['name']['ru_long_form']
            country.local_long_name = data['name']['local_long_form']
            country.local_short_name = data['name']['local_short_form']
            country.en_long_form = data['name']['en_long_form']
            country.en_short_form = data['name']['en_short_form']
            country.ru_name_rod = data['name']['ru_name_rod']
            country.ru_name_dat = data['name']['ru_name_dat']
            country.ru_name_vin = data['name']['ru_name_vin']
            country.ru_name_tvo = data['name']['ru_name_tvo']
            country.ru_name_pre = data['name']['ru_name_pre']

            country.continent = data['region']
            country.exact_region = data['exact_region']

            country.capital_name = data['ru_capital']
            country.en_capital_name = data['capital']

            country.anthem = data['anthem_audio_url']
            country.internet_tld = data['internet_tld']

            country.iso_code_a2 = data['iso_2']
            country.iso_code_a3 = data['iso_3']
            country.iso_code_num = data['iso_num']

            country.ru_government_type = data['ru_government_type']
            country.en_government_type = data['en_government_type']

            country.ru_chief_of_state = data['ru_chief_of_state']
            country.en_chief_of_state = data['en_chief_of_state']

            country.ru_head_of_government = data['ru_head_of_government']
            country.en_head_of_government = data['en_head_of_government']
            country.area_total = data['area']['total']
            country.land_area = data['area']['land']
            country.water_area = data['area']['water']
            country.coastline = data['coastline']
            country.area_global_rank = data['area']['global_rank']

            country.population_total = data['population']['total']
            country.population_date = data['population']['date']
            country.population_global_rank = data['population']['global_rank']

            country.gdp_value = data['gdp']['value']
            country.gdp_date = data['gdp']['date']
            country.gdp_global_rank = data['gdp']['global_rank']

            country.external_debt_value = data['external_debt']['value']
            country.external_debt_date = data['external_debt']['date']
            country.external_debt_global_rank = data['external_debt']['global_rank']

            country.info_updated = data['updated']

            country.save()

            print(f'+[Success] Country {country} created!')
            print(f'Go to create flag with {iso_code_a2} and {country.ru_name_rod}:')

            create_flag(country)


def add_border_countries():
    # country.border_countries
    pass
