#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Self, Any, Optional
from Interfaces.FormatModuleInterface import FormatModuleInterface
from Utils.Patterns import RE
from Utils.Check import Check
from Utils.Utils import Utils

class SerieFormat:

    def __init__(self:Self, 
        format:FormatModuleInterface, 
        options:Optional[dict[str, Any|None]] = None
    ) -> None:
        self.format:FormatModuleInterface = format

    def get(self:Self, 
        i:int, 
        inputs:str|list[Any|None], 
        shared:dict[str, Any|None] = {}, 
        fragments:list[str] = []
    ) -> str:
        
        inputs = (
            [
                int(value) for value in str(Utils.get_random(inputs.split("|"))).split("-")
            ] if Check.is_string(inputs) else 
            inputs if Check.is_array(inputs) else
        [])
        serie:int|None = shared.get("serie")
        range:int|list[int]|list[int, int]

        if serie is not None:
            inputs = [range for range in inputs if (
                serie < range if not Check.is_array(range) else
                serie < range[0] if len(range) == 1 else 
            serie < range[1])]

        range = Utils.get_random(*inputs)

        if not Check.is_array(range):
            shared["serie"] = range
            return str(range)
        if len(range) == 1:
            shared["serie"] = range[0]
            return str(range[0])
        
        value:int

        while True:
            value = Utils.get_random(*range)
            if value > serie:
                break
        serie = value

        return str(value)
    
    def check(self:Self, 
        i:int, 
        string:str, 
        inputs:str|list[Any|None], 
        shared:dict[str, Any|None] = {}, 
        fragments:list[str] = []
    ) -> int:
        return self.format.check_range(string, inputs)