from typing import List
from typing import Tuple
import random
import math


def split_train_dev(
        game_files: List[str], train_ratio: float = 0.9, rnd_seed: int = 42
) -> Tuple[List[str], List[str]]:
    """
    Split train/dev sets from given game files
    sort - shuffle w/ Random(42) - split

    Args:
        game_files: game files
        train_ratio: the percentage of training files
        rnd_seed: for randomly shuffle files, default = 42

    Returns:
        train_games, dev_games

    Exception:
        empty game_files
    """

    # have to sort first, otherwise after shuffling the result is different
    # on different platforms, e.g. Linux VS MacOS.
    game_files = sorted(game_files)
    random.Random(rnd_seed).shuffle(game_files)
    n_files = len(game_files)
    if n_files == 0:
        raise ValueError("no game files found!")

    n_train = int(math.ceil(n_files * train_ratio))
    n_dev = n_files * (1 - train_ratio)
    n_dev = int(math.floor(n_dev)) if n_dev > 1 else 1
    train_games = game_files[:n_train]
    dev_games = game_files[-n_dev:]
    return train_games, dev_games