# coding:utf8

__author__ = 'dong'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob
from xml.etree.ElementTree import ElementTree, Element
import distutils.dir_util
from xml.etree import ElementTree as ET
import gw_file_system
import gw_data_center

ANDROID_NS = 'http://schemas.android.com/apk/res/android'


def script_init(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    # gw_data_center.pack_small=True
    return


def script_second(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    list_dpi = ["drawable-mdpi", "drawable-hdpi", "drawable-xhdpi","drawable-xxhdpi","drawable-xxxhdpi"]
    list_png = [
        "abc_ab_share_pack_mtrl_alpha", "abc_btn_switch_to_on_mtrl_00001", "abc_btn_switch_to_on_mtrl_00012",
        "abc_cab_background_top_mtrl_alpha", "abc_list_divider_mtrl_alpha", "abc_list_focused_holo",
        "abc_list_longpressed_holo",
        "abc_list_pressed_holo_dark", "abc_list_pressed_holo_light", "abc_list_selector_disabled_holo_dark",
        "abc_list_selector_disabled_holo_light",
        "abc_menu_hardkey_panel_mtrl_mult", "abc_popup_background_mtrl_mult", "abc_scrubber_primary_mtrl_alpha",
        "abc_scrubber_track_mtrl_alpha",
        "abc_spinner_mtrl_am_alpha", "abc_switch_track_mtrl_alpha", "abc_tab_indicator_mtrl_alpha",
        "abc_textfield_activated_mtrl_alpha",
        "abc_textfield_default_mtrl_alpha", "abc_textfield_search_activated_mtrl_alpha",
        "abc_textfield_search_default_mtrl_alpha",
    ]
    list_xml=[
            "abc_edit_text_material","abc_item_background_holo_dark","abc_item_background_holo_light","abc_list_selector_background_transition_holo_dark",
              "abc_list_selector_background_transition_holo_light","abc_list_selector_holo_dark","abc_list_selector_holo_light","abc_seekbar_track_material",
              "abc_spinner_textfield_background_material","abc_switch_thumb_material","abc_tab_indicator_material","abc_textfield_search_material",
              ]
    list_style=["listChoiceBackgroundIndicator","editTextBackground","selectableItemBackground"]
    public_path = os.path.join(extract_dir, "../sdk/smallsheep/ForRes/values/public.xml")
    style_path = os.path.join(extract_dir, "../sdk/smallsheep/ForRes/values/styles.xml")
    for png in list_png:
        for dpi in list_dpi:
            png_path = os.path.join(extract_dir, "../sdk/smallsheep/ForRes", dpi, png+".9.png")
            logging.info(png_path)
            if os.path.exists(png_path):
                os.remove(png_path)
                handle_public_xml(public_path, png)
                handle_public_xml(style_path, png)
    for xml in list_xml:
        xml_path=os.path.join(extract_dir,"../sdk/smallsheep/ForRes/drawable",xml+".xml")
        logging.info(xml_path)
        if os.path.exists(xml_path):
            os.remove(xml_path)
            handle_public_xml(public_path, xml)
            handle_public_xml(style_path,xml)

    for style in list_style:
        handle_public_xml(style_path,style)

def handle_public_xml(public_xml_path, remove_node):
    if not os.path.exists(public_xml_path):
        logging.info('public_xml is null: ' + public_xml_path)
        return
    new_lines = []
    with open(public_xml_path, 'r+') as f:
        for line in f.readlines():
            # l = line.strip()
            if line.find(remove_node) > -1:
                continue
            new_lines.append(line)
        f.seek(0)
        f.truncate()
        f.writelines(new_lines)
    return


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info("-----debug-----")


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    return


if __name__ == '__main__':
    # replace_super_application("D:\work\wzdq")
    ""
