import os, sys, unidecode
from time import sleep

from pprint import pprint
from unicodedata import normalize
from termcolor import colored

from bestia import *

def flatten_string(string):
    old_char = ''
    new_char = bytearray()
    for c in string:
        b = bytearray(c, ENCODING)
        if sys.getsizeof(c) != CHAR_SIZE:
            old_char += c
            unicode_code_point = ord(c) # int
            new_char.append(unicode_code_point)
    new_char = new_char.decode()
    return string.replace(old_char, new_char)


def flatten_char(c):
    size = sys.getsizeof(c)
    if size != CHAR_SIZE:
        color = 'red'
        symbol = '!='
    else:
        color = 'green'
        symbol = '=='
    echo('{} | size: {} {} {} | utf_code_point: {}'.format(c, size, symbol, CHAR_SIZE, ord(c)), color)


def redecode_unicode_chars(input_string):
    # change replace_special_chars with this
    suspects = [
        'Ã', 
        'Â',
        'Å',
        'Ä',
        # 'Ã®',
    ]
    output_string = ''
    detected = False
    new_char = bytearray()
    for c in input_string:
        size = sys.getsizeof(c)
        # CHAR SIZE IS NOT A GOOD IDICATOR OF WHEN WE WILL FIND
        # if size == CHAR_SIZE and not detected:
        if c not in suspects and not detected:
            echo('standard char: {}'.format(c))
            output_string += c

        elif c in suspects and not detected:
            echo('1 suspect char: {}'.format(c))
            flatten_char(c)
            detected = True
            unicode_code_point = ord(c) # int
            new_char.append(unicode_code_point)

        elif size != CHAR_SIZE and detected:
            echo('2 suspect char: {}'.format(c))
            flatten_char(c)
            unicode_code_point = ord(c) # int
            new_char.append(unicode_code_point)

            detected = False
            output_string += new_char.decode(ENCODING)
            new_char = bytearray()
            # this assumes that I will be encoding chars in chunks of length 2... is that fair ????

        # echo(output_string)
    return output_string


d = {
    'a': 1,
    'a2': {
        'b5': 29,
        'b2': 25,
        'bp': 2,
    },
    'a3': 1,
    'a4': 1,
    'a5': 1,
    'a6': 1,
    'a7': 1,
}


# Dumbo DVDRip [dublat romana]
# zburător
# cunoştinţă
# descoperă
rumeno = '''Faceãi cunostinãÄƒ cu Dumbo, puiul cel mititel ÅŸi dulce al Doamnei Jumbo, care Ã®i farmecÄƒ pe to
ãi cei care Ã®l vÄƒd... pânÄƒ când lumea descoperÄƒ cÄƒ are niÅŸte urechi mari ÅŸi clÄƒpÄƒuge.

Ajutat de cel mai bun prieten al lui, ÅŸoricelul Timothy, Dumbo Ã®ÅŸi dÄƒ seama Ã®n scurtÄƒ vreme cÄ
ƒ urechile lui spectaculoase Ã®l fac sÄƒ fie un personaj unic, cu totul deosebit, care poate deveni
celebru Ã®n chip de unic elefant zburÄƒtor al lumii.'''

print(replace_special_chars(rumeno))
# ƒ ord() returns 402 ... out of range
# Ÿ | size: 76 != 50 | utf_code_point: 376

sys.exit()





# s = 'NewellÂ´s'
# print(s)
# print(redecode_unicode_chars(s))
# print()
# s = 'Real Madrid vs AtlÃ©tico de Madrid'
# print(s)
# print(redecode_unicode_chars(s))
# print()
# s = 'Macarálo'
# print(s)
# print(redecode_unicode_chars(s))
# print()
# s = 'ú'
# print(s)
# print(redecode_unicode_chars(s))
# print()
# s = 'Núñez'
# print(s)
# print(redecode_unicode_chars(s))
# print()
# s = 'EspaÃ±oletación'
# print(s)
# print(redecode_unicode_chars(s))
# print()

# how to separate ú from © ??
# Å£Äƒ	are these 1 or 2 chars ? surely not 1 ... ?
