# 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_apk_tool

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


def script_second(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    logging.info('script_second: ' + str(channel_sdk_info))
    # 逆向渠道id

    if  channel_sdk_info['id'] == '597':
        public_xml = os.path.join(extract_dir, "res/values/public.xml")
        handle_public_xml(public_xml, "attr-private")
        handle_public_xml(public_xml, "mtrl_layout_snackbar")

        public_xml = os.path.join(extract_dir, "res/values/styles.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/values-v21/styles.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/layout/design_bottom_sheet_dialog.xml")
        handle_public_xml(public_xml, "bottomSheetStyle")

        public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar.xml")
        os.remove(public_xml)
        # handle_public_xml(public_xml, "snackbarStyle")

        public_xml = os.path.join(extract_dir, "res/layout-sw600dp/mtrl_layout_snackbar.xml")
        os.remove(public_xml)
        # handle_public_xml(public_xml, "snackbarStyle")

        public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar_include.xml")
        handle_public_xml(public_xml, "textAppearanceBody2")


        public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar_include.xml")
        handle_public_xml(public_xml, "snackbarButtonStyle")

    # handle_public_xml(public_xml, "sobot_fileEndingImage")
    # handle_public_xml(public_xml, "sobot_fileEndingPPT")
    # handle_public_xml(public_xml, "sobot_fileEndingPackage")
    # handle_public_xml(public_xml, "sobot_fileEndingPdf")
    # handle_public_xml(public_xml, "sobot_fileEndingText")
    # handle_public_xml(public_xml, "sobot_fileEndingVideo")
    # handle_public_xml(public_xml, "sobot_fileEndingWord")

    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):
    if channelSdkInfo['id'] == '597':
        move(decompileDir)
    else:
        return

def move(decompileDir):
    s1 = os.path.join(decompileDir, "smali/androidx")
    smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
    s3 = os.path.join(decompileDir, smali_classes_new, "androidx")
    list_package = ["activity","arch","constraintlayout","cursoradapter","drawerlayout","legacy"]

    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    gw_apk_tool.move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, "smali/androidx")
    smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
    s3 = os.path.join(decompileDir, smali_classes_new, "androidx")
    list_package = ["localbroadcastmanager","annotation","asynclayoutinflater","coordinatorlayout"]

    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    gw_apk_tool.move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, "smali/androidx")
    smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
    s3 = os.path.join(decompileDir, smali_classes_new, "androidx")
    list_package = ["customview","fragment","lifecycle","appcompat","collection","core","documentfile","interpolator","loader"]

    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    gw_apk_tool.move_package(s1, s3, list_package)




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





if __name__ == '__main__':
    # replace_super_application("D:\work\wzdq")
    modify_manifest("E:\\apk\\youhua\\youhua.zip.out", "requestLegacyExternalStorage")
