import json
import pkg_resources

class RhymeChecker:
    def __init__(self, json_file_path=None):
        if json_file_path is None:
            json_file_path = pkg_resources.resource_filename(
                __name__, 'data/organized_ping_ze_rhyme_dict.json'
            )

        with open(json_file_path, 'r', encoding='utf-8') as file:
            self.ping_ze_dict = json.load(file)

        self.rhyme_dict = self._build_rhyme_dict()

    def _build_rhyme_dict(self):
        """
        Builds a dictionary where the keys are characters, and the values are lists of their rhyme groups.
        """
        rhyme_dict = {}
        for tone_type in self.ping_ze_dict:
            for tone_group in self.ping_ze_dict[tone_type]:
                for rhyme_category, characters in self.ping_ze_dict[tone_type][tone_group].items():
                    for char_group in characters:
                        for char in char_group:
                            rhyme_group = (tone_type, tone_group, rhyme_category)
                            if char in rhyme_dict:
                                rhyme_dict[char].append(rhyme_group)
                            else:
                                rhyme_dict[char] = [rhyme_group]
        return rhyme_dict
    
    def get_rhyme_group(self, char):
        """
        Returns the list of rhyme groups for a given character, or None if the character is not found.
        """
        return self.rhyme_dict.get(char)

    def do_rhyme(self, char1, char2):
        """
        Determines if two characters rhyme by comparing their rhyme groups and tone types.

        In the instance that a character has multiple rhyme groups due to multiple pronunciations, it returns true if it has one rhyme group in common with the other character.

        e.g. "鄉" is in 下平聲七陽 and 去聲二十三漾, it is assumed to use the rhyming pronunciation when compared to "昌" in 下平聲七陽
        """
        rhyme_groups1 = self.get_rhyme_group(char1)
        rhyme_groups2 = self.get_rhyme_group(char2)

        if not rhyme_groups1 or not rhyme_groups2:
            return False  # One or both characters are not in the rhyme data

        # Extract the core rhyme categories for each character (e.g., '上平聲一東' -> '一東')
        core_rhymes1 = {group[2].split('聲')[-1] for group in rhyme_groups1}
        core_rhymes2 = {group[2].split('聲')[-1] for group in rhyme_groups2}
        
        # Return True if there is any intersection between the two sets of core rhymes
        return not core_rhymes1.isdisjoint(core_rhymes2)
        
    def get_rhyme_type(self, char):
        """
        Returns the full list of rhyme types of the given character.
        """
        return self.get_rhyme_group(char)
