import os
import json
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.clickjacking import xframe_options_exempt

cur_dir = os.path.dirname(os.path.realpath(__file__))


TARGET_COUNTRY = settings.TARGET_COUNTRY


@xframe_options_exempt
def home(request):

    location = TARGET_COUNTRY.lower()

    config_path = os.path.join(cur_dir, 'static', 'config', '{}.json'.format(location))
    if not os.path.exists(config_path):
        return HttpResponse('config json file for {} not found'.format(location))

    with open(config_path, 'r') as f:
        config = json.load(f)

    map_set = list(config.keys())[0]
    print ("map_set: ", map_set)
    default_options = [list(values)[0] for key, values in config[map_set].items() if key != 'name']
    default_image_name = '_'.join(default_options)
    print ("default_image_name: ", default_image_name)

    image_url = os.path.join('ml_static', 'maps', location, map_set, default_image_name + '.png')
    print (image_url)
    
    # image_url = os.path.join('../static', 'maps', location, map_set, 'sample.png')

    context = {
        'location': location,
        'config': config,
        'image_url': image_url,
    }

    return render(request, 'index.html', context)


