# -*- coding: utf-8 -*-

"""Script to encode all texts in the game."""

import logging

import data
import utils


def main():
    # logging.basicConfig(level=logging.DEBUG, format='%(message)s')
    logging.basicConfig(level=logging.WARN, format='%(message)s')

    trans_table = utils.translation_table()
    max_len = 128 - 4 - 3

    with open('/home/michal/dos/PUDR/PUDR/DATA.PAS', mode='w',
              encoding='latin1', newline='\r\n') as f:

        print('{generated by tools/encode_texts.py}\n', file=f)

        # generate texts
        for section_name, section_texts in data.texts.items():
            print(f'{section_name}: array[0..{len(section_texts) - 1}]'
                  f' of string[{max(len(t) for t in section_texts)}] = (',
                  file=f)

            output = []

            for item in section_texts:
                if isinstance(item, list):
                    output_line = ''.join(chr(n + 48) for n in item)
                else:
                    wrapped = list(utils.wrap_text(item))
                    logging.debug('{}\n{}'.format('\n'.join(wrapped), '-'*27))
                    if len(wrapped) > 6 or (section_name == 'texts_other'
                                            and len(wrapped) > 4):
                        logging.warning('TOO MANY LINES:\n{}'
                                        .format('\n'.join(wrapped)))
                    output_line = '_'.join(wrapped).translate(trans_table)

                if len(output_line) > max_len:
                    # divide to fit in Turbo Pascal IDE's max line length 128
                    output_line = f"{output_line[:max_len]}'\n" \
                                  f"    + '{output_line[max_len:]}"
                    # because of this, can't use {!r} on the following line

                output.append(f'    \'{output_line}\'')

            print(',\n'.join(output), file=f)
            print('\n);\n', file=f)

        # generate things
        thing_count = len(data.things) - 1
        print(f'things: array[0..{thing_count}] of thing = (', file=f)

        output = []

        for thing in data.things:
            name = thing['name'].translate(trans_table)

            img_name = thing['image'][:8]

            wrapped = list(utils.wrap_text(thing['description']))
            logging.debug('{}\n{}'.format('\n'.join(wrapped), '-' * 27))
            if len(wrapped) > 4:
                logging.warning('TOO MANY LINES:\n{}'
                                .format('\n'.join(wrapped)))
            desc = '_'.join(wrapped).translate(trans_table)

            output.append(f"  (name: '{name}'; where: {thing['where']};"
                          f" portable: {thing['portable']};"
                          f" slot: {thing['slot']};"
                          f"\n   desc: '{desc}';\n   image: @{img_name})")

        print(',\n\n'.join(output), file=f)
        print('\n);\n', file=f)
        print(f'thing_count: Byte = {thing_count};\n', file=f)

        # report length stats
        # max_exit_len = max(len(item) for item in data.texts['exit_names'])
        # max_exit_len_items = [utils.strip_diacritics(item)
        #                       for item in data.texts['exit_names']
        #                       if len(item) == max_exit_len]
        # print(f'{{\nmax. exit name length: {max_exit_len}\n'
        #       f'{max_exit_len_items}\n', file=f)

        # max_name_len = max(len(item['name']) for item in data.things)
        # max_name_len_items = [utils.strip_diacritics(item['name'])
        #                       for item in data.things
        #                       if len(item['name']) == max_name_len]
        # print(f'max. thing name length: {max_name_len}\n'
        #       f'{max_name_len_items}\n', file=f)

        # max_desc_len = max(len(item['description']) for item in data.things)
        # max_desc_len_items = [utils.strip_diacritics(item['description'])
        #                       for item in data.things
        #                       if len(item['description']) == max_desc_len]
        # print(f'max. thing description length: {max_desc_len}\n'
        #       f'{max_desc_len_items}\n}}', file=f)


if __name__ == '__main__':
    main()

# -*- coding: utf-8 -*-

"""Script to encode all texts in the game."""

import logging

import data
import utils


def main():
    # logging.basicConfig(level=logging.DEBUG, format='%(message)s')
    logging.basicConfig(level=logging.WARN, format='%(message)s')

    trans_table = utils.translation_table()
    max_len = 128 - 4 - 3

    with open('/home/michal/dos/PUDR/PUDR/DATA.PAS', mode='w',
              encoding='latin1', newline='\r\n') as f:

        print('{generated by tools/encode_texts.py}\n', file=f)

        # generate texts
        for section_name, section_texts in data.texts.items():
            print(f'{section_name}: array[0..{len(section_texts) - 1}]'
                  f' of string[{max(len(t) for t in section_texts)}] = (',
                  file=f)

            output = []

            for item in section_texts:
                if isinstance(item, list):
                    output_line = ''.join(chr(n + 48) for n in item)
                else:
                    wrapped = list(utils.wrap_text(item))
                    logging.debug('{}\n{}'.format('\n'.join(wrapped), '-'*27))
                    if len(wrapped) > 6 or (section_name == 'texts_other'
                                            and len(wrapped) > 4):
                        logging.warning('TOO MANY LINES:\n{}'
                                        .format('\n'.join(wrapped)))
                    output_line = '_'.join(wrapped).translate(trans_table)

                if len(output_line) > max_len:
                    # divide to fit in Turbo Pascal IDE's max line length 128
                    output_line = f"{output_line[:max_len]}'\n" \
                                  f"    + '{output_line[max_len:]}"
                    # because of this, can't use {!r} on the following line

                output.append(f'    \'{output_line}\'')

            print(',\n'.join(output), file=f)
            print('\n);\n', file=f)

        # generate things
        thing_count = len(data.things) - 1
        print(f'things: array[0..{thing_count}] of thing = (', file=f)

        output = []

        for thing in data.things:
            name = thing['name'].translate(trans_table)

            img_name = thing['image'][:8]

            wrapped = list(utils.wrap_text(thing['description']))
            logging.debug('{}\n{}'.format('\n'.join(wrapped), '-' * 27))
            if len(wrapped) > 4:
                logging.warning('TOO MANY LINES:\n{}'
                                .format('\n'.join(wrapped)))
            desc = '_'.join(wrapped).translate(trans_table)

            output.append(f"  (name: '{name}'; where: {thing['where']};"
                          f" portable: {thing['portable']};"
                          f" slot: {thing['slot']};"
                          f"\n   desc: '{desc}';\n   image: @{img_name})")

        print(',\n\n'.join(output), file=f)
        print('\n);\n', file=f)
        print(f'thing_count: Byte = {thing_count};\n', file=f)

        # report length stats
        # max_exit_len = max(len(item) for item in data.texts['exit_names'])
        # max_exit_len_items = [utils.strip_diacritics(item)
        #                       for item in data.texts['exit_names']
        #                       if len(item) == max_exit_len]
        # print(f'{{\nmax. exit name length: {max_exit_len}\n'
        #       f'{max_exit_len_items}\n', file=f)

        # max_name_len = max(len(item['name']) for item in data.things)
        # max_name_len_items = [utils.strip_diacritics(item['name'])
        #                       for item in data.things
        #                       if len(item['name']) == max_name_len]
        # print(f'max. thing name length: {max_name_len}\n'
        #       f'{max_name_len_items}\n', file=f)

        # max_desc_len = max(len(item['description']) for item in data.things)
        # max_desc_len_items = [utils.strip_diacritics(item['description'])
        #                       for item in data.things
        #                       if len(item['description']) == max_desc_len]
        # print(f'max. thing description length: {max_desc_len}\n'
        #       f'{max_desc_len_items}\n}}', file=f)


if __name__ == '__main__':
    main()

# -*- coding: utf-8 -*- Script to encode all texts in the game. # logging.basicConfig(level=logging.DEBUG, format='%(message)s') # generate texts # divide to fit in Turbo Pascal IDE's max line length 128 # because of this, can't use {!r} on the following line # generate things # report length stats # max_exit_len = max(len(item) for item in data.texts['exit_names']) # max_exit_len_items = [utils.strip_diacritics(item) #                       for item in data.texts['exit_names'] #                       if len(item) == max_exit_len] # print(f'{{\nmax. exit name length: {max_exit_len}\n' #       f'{max_exit_len_items}\n', file=f) # max_name_len = max(len(item['name']) for item in data.things) # max_name_len_items = [utils.strip_diacritics(item['name']) #                       for item in data.things #                       if len(item['name']) == max_name_len] # print(f'max. thing name length: {max_name_len}\n' #       f'{max_name_len_items}\n', file=f) # max_desc_len = max(len(item['description']) for item in data.things) # max_desc_len_items = [utils.strip_diacritics(item['description']) #                       for item in data.things #                       if len(item['description']) == max_desc_len] # print(f'max. thing description length: {max_desc_len}\n' #       f'{max_desc_len_items}\n}}', file=f)