#!/usr/bin/python3

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

import os
import os.path
import sys
import codecs
from argparse import ArgumentParser
from icu import UNICODE_VERSION, ICU_VERSION, VERSION

try:
    from wstools.dbl import DBL
except ImportError:
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib', 'wstools')))
    from dbl import DBL


def main():
    parser = ArgumentParser(
        description='Find exemplars for LDML files')
    parser.add_argument('project', nargs='+', help='DBL project zip file')
    parser.add_argument('--version', action='version',
                        version='%(prog)s: Unicode: {} ICU: {} PyICU: {}'.format(UNICODE_VERSION,
                                                                                 ICU_VERSION,
                                                                                 VERSION))
    args = parser.parse_args()

    dbl = DBL()

    # The following are examples of how to set the lists of exemplars
    # from content taken from a LDML file. Other settings can be changed
    # as well. If a LDML file has no exemplar information for a particular set,
    # there is no need to initialize the Exemplar class with an empty set.

    # User settable configuration.
    dbl.exemplars.many_bases = 5
    dbl.exemplars.frequent = 0

    # From existing LDML files.
    dbl.exemplars.main = '[]'
    dbl.exemplars.auxiliary = '[]'
    dbl.exemplars.index = '[]'
    dbl.exemplars.punctuation = '[]'
    dbl.exemplars.digits = '[]'

    # Find exemplars in the data.
    data_filename = ''
    for project in args.project:
        data_filename = os.path.normcase(project)
        (base_filename, src_type) = os.path.splitext(data_filename)
        src_type = src_type.lower()
        if src_type == '.zip':
            dbl.open_project(data_filename)
            # dbl.query_project()
            dbl.process_project()
            dbl.close_project()
        else:
            with codecs.open(project, 'r', encoding='utf_8_sig') as source:
                for line in source:
                    dbl.exemplars.process(line)
    dbl.analyze_projects()

    # Display the exemplars.
    with codecs.open(base_filename + '.ldml', 'w', encoding='utf-8') as ldml_file:
        ldml_file.write(' '.join(args.project) + '\n')
        ldml_file.write('main        {}\n'.format(dbl.exemplars.main))
        ldml_file.write('auxiliary   {}\n'.format(dbl.exemplars.auxiliary))
        ldml_file.write('index       {}\n'.format(dbl.exemplars.index))
        ldml_file.write('punctuation {}\n'.format(dbl.exemplars.punctuation))
        ldml_file.write('digits      {}\n'.format(dbl.exemplars.digits))
        ldml_file.write('graphemes   {}\n'.format(dbl.exemplars.graphemes))
        ldml_file.write('frequency   {}\n'.format(dbl.exemplars.frequency))
        ldml_file.write('script      {}\n'.format(dbl.exemplars.script))
        raw_clusters = dbl.exemplars.raw_clusters
        both = list()
        for exemplar, count in raw_clusters.most_common():
            both.append('{}={}'.format(exemplar.text, count))
        spreadsheet = ' '.join(both)
        ldml_file.write('object      {}\n'.format(spreadsheet))


if __name__ == '__main__':
    main()
