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

# 导出哪些翻译未翻译(找出个别的几个没有翻译, 或者多余的词条)

import os
from optparse import OptionParser
from xml.dom import minidom
import pyExcelerator

APP_MOUDULE = 'app'
FIND_DIDS = [APP_MOUDULE, 'frame', 'module']
LANGUAGE_CODE = ['', 'zh', 'ar', 'hi', 'tr', 'th']


def option_parser():
    parser = OptionParser()
    parser.add_option("-o", "--outputDir",
                      help="输出路径",
                      metavar="outputDir")
    (options, _) = parser.parse_args()
    return options


def read_xml_2_dom(path):
    f = open(path, 'r')
    try:
        dom = minidom.parse(f)
        f.close()
        return dom
    except:
        print('xml_2_dom error, path:{}'.format(path))
        f.close()
        return


def read_strings_2_dict(strings_path):
    string_dict = {}
    if not os.path.exists(strings_path):
        return string_dict

    dom = read_xml_2_dom(strings_path)
    if dom == None:
        return

    root = dom.documentElement
    strings = root.getElementsByTagName('string')
    for string in strings:
        translatable = string.getAttribute('translatable')
        if translatable == 'false':
            continue
        key = string.getAttribute('name')
        # print("read_strings_2_dict, key:{0}".format(key))
        value = string.firstChild.data
        string_dict[key] = value
    return string_dict


def module_export_untranslate(module_name, module_dir, output_dir):
    print("module_export_untranslate, module_name:{0}, module_dir:{1}".format(
        module_name, module_dir))
    all_en_strings = os.path.join(
        module_dir, 'src/main/res/values/strings.xml')
    all_zh_strings = os.path.join(
        module_dir, 'src/main/res/values-zh/strings.xml')
    all_ar_strings = os.path.join(
        module_dir, 'src/main/res/values-ar/strings.xml')
    all_hi_strings = os.path.join(
        module_dir, 'src/main/res/values-hi/strings.xml')
    all_tr_strings = os.path.join(
        module_dir, 'src/main/res/values-tr/strings.xml')
    all_th_strings = os.path.join(
        module_dir, 'src/main/res/values-th/strings.xml')

    en_strings_dict = read_strings_2_dict(all_en_strings)
    zh_strings_dict = read_strings_2_dict(all_zh_strings)
    ar_strings_dict = read_strings_2_dict(all_ar_strings)
    hi_strings_dict = read_strings_2_dict(all_hi_strings)
    tr_strings_dict = read_strings_2_dict(all_tr_strings)
    th_strings_dict = read_strings_2_dict(all_th_strings)

    all_keys = set()
    all_keys.update(en_strings_dict.keys())
    all_keys.update(zh_strings_dict.keys())
    all_keys.update(ar_strings_dict.keys())
    all_keys.update(hi_strings_dict.keys())
    all_keys.update(tr_strings_dict.keys())
    all_keys.update(th_strings_dict.keys())

    module_untranslate_list = [['keyName', 'en', 'zh', 'ar', 'hi', 'tr', 'th']]

    for key in all_keys:
        if not en_strings_dict.has_key(key) or not zh_strings_dict.has_key(key) or not ar_strings_dict.has_key(
                key) or not hi_strings_dict.has_key(key) or not tr_strings_dict.has_key(key) or not th_strings_dict.has_key(key):
            en_value = en_strings_dict.get(key)
            zh_value = zh_strings_dict.get(key)
            ar_value = ar_strings_dict.get(key)
            hi_value = hi_strings_dict.get(key)
            tr_value = tr_strings_dict.get(key)
            th_value = th_strings_dict.get(key)
            module_untranslate_list.append([key, en_value, zh_value, ar_value, hi_value, tr_value, th_value])

    if len(module_untranslate_list) <= 1:
        return

    workbook = pyExcelerator.Workbook()
    ws = workbook.add_sheet(module_name)
    row_index = 0
    for row in module_untranslate_list:
        column_index = 0
        print(row)
        for column in row:
            # print("module_export_untranslate, row_index:{0}, column_index:{1}, column:{2}".format(
            #     row_index, column_index, column.encode('utf-8')))
            columnStr = column
            if column is None:
                columnStr = ""
            ws.write(row_index, column_index, columnStr)
            column_index += 1
        row_index += 1
    output_path = os.path.join(output_dir, '{}.xls'.format(module_name))
    workbook.save(output_path)


def export_untranslate(options):
    output_dir = options.outputDir
    cwd = os.getcwd()
    print("export_untranslate, cwd:{0}, output_dir:${1}".format(cwd, output_dir))
    for dir in FIND_DIDS:
        full_dir = os.path.join(cwd, dir)
        if not os.path.exists(full_dir):
            continue
        if dir == APP_MOUDULE:
            module_export_untranslate(APP_MOUDULE, full_dir, output_dir)
        else:
            for module in os.listdir(full_dir):
                module_export_untranslate(
                    module, os.path.join(full_dir, module), output_dir)


options = option_parser()
export_untranslate(options)
