from ast import literal_eval
from os import listdir, path, remove

import pygame as pg

from ...render.menu_rendering import SAVES_FOLDER
from ...tile_systems.serialize import deserialize_chunks

def save_loading(state, chunks):
    if state.position[1] <= 50:
        state.menu_placement = "main_menu"
    elif state.position[1] <= 100:
        state.menu_placement = "save_creation"
    else:
        saves = [f[:-4] for f in listdir(SAVES_FOLDER) if f.endswith(".txt")]
        index = (state.position[1] // 50) - 2
        if index < len(saves):
            state.save_file_name = saves[index]
            if state.position[0] >= 120:
                state.menu_placement = "main_game"
                with open(path.join(SAVES_FOLDER, state.save_file_name + ".txt"), "r", encoding="utf-8") as file:
                    file_content = file.read().split(";")
                chunks = deserialize_chunks(file_content[0])
                state.location["tile"] = literal_eval(file_content[1])
                state.tick = int(file_content[2])
                state.location["room"] = literal_eval(file_content[3])
                state.location["real"] = list(state.location["tile"])
                state.noise_offset = literal_eval(file_content[4])
            elif state.position[0] <= 90:
                file_path = path.join(SAVES_FOLDER, state.save_file_name + ".txt")
                if path.exists(file_path):
                    remove(file_path)
    return chunks