# coding:utf8
import os
import logging
import shutil
from xml.etree import ElementTree as ET
import traceback
import distutils.dir_util
import re
import gw_report_tool
import gw_data_center
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):
    # split(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info)
    return


def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    # handle_same_package(extract_dir, channel_sdk_info)
    return


def script(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    # smali_classes2
    # split(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info)
    return


def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    # smali_classes2

    sdk_id = channel_sdk_info['id']
    if sdk_id == "512" or sdk_id=="597":
         # smali_classes6
        s1 = os.path.join(extract_dir, "smali_classes2", "androidx")
        smali_classes2_dir = create_mutil_smali(extract_dir)
        s3 = os.path.join(smali_classes2_dir, "androidx")
        list_package = ["annotation","appcompat","arch","asynclayoutinflater","cardview","collection","core","constraintlayout","customview","cursoradapter","coordinatorlayout","databinding"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)



def split(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    s1 = os.path.join(extract_dir, "smali_classes2")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = smali_classes2_dir
    list_package = ["androidx"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3
    s1 = os.path.join(extract_dir, "smali_classes3")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir)
    list_package = ["androidx"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes4
    s1 = os.path.join(extract_dir, "smali_classes4", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["dataeye", "dylanvann", "example", "facebook", "github", "hw", "itrabbit", "lmy"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    s1 = os.path.join(extract_dir, "smali_classes4")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir)
    list_package = ["androidx"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    s1 = os.path.join(extract_dir, "smali_classes4", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["netease" ]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes5
    s1 = os.path.join(extract_dir, "smali_classes5", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["dataeye", "dylanvann", "example", "facebook", "github", "hw", "itrabbit", "lmy"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    s1 = os.path.join(extract_dir, "smali_classes5", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["netease" ]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    s1 = os.path.join(extract_dir, "smali_classes5")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir)
    list_package = ["androidx"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes6
    s1 = os.path.join(extract_dir, "smali_classes6", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["dylanvann", "example", "facebook", "github", "hw", "itrabbit", "lmy"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes2
    s1 = os.path.join(extract_dir, "smali_classes2", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["airbnb", "coremedia", "google", "JavaWebsocket", "netease", "reactnativecommunity",
                    "shahenlibrary", "tencent", "yulong", "baidu", "facebook", " googlecode", "mp4parser",
                    "reactnative", "scwang"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-11
    s1 = os.path.join(extract_dir, "smali_classes3", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["airbnb", "android", "bumptech", "CCMsgSdk", "coremedia"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-12
    s1 = os.path.join(extract_dir, "smali_classes3", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["mojang", "mp4parser", "netease", "random", "reactnativecommunity", "RNFetchBlob"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-13
    s1 = os.path.join(extract_dir, "smali_classes3", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["example", "facebook"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-14
    s1 = os.path.join(extract_dir, "smali_classes3", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["hw", "itrabbit", "JavaWebsocket", "lmy"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-15
    s1 = os.path.join(extract_dir, "smali_classes3", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["github", "google", "googlecode"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-16
    s1 = os.path.join(extract_dir, "smali_classes3", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["scwang", "sensors", "shahenlibrary", "sina", "swmansion", "tencent", "yalantis", "yqritc",
                    "yulong", "zmxv"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-17
    s1 = os.path.join(extract_dir, "smali_classes3")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = smali_classes2_dir
    list_package = ["android", "androidx/legacy", "androidx/lifecycle", "androidx/loader",
                    "androidx/localbroadcastmanager", "androidx/media", "androidx/print", "androidx/savedstate",
                    "androidx/slidingpanelayout",
                    "androidx/swiperefreshlayout", "androidx/vectordrawable", "androidx/versionedparcelable"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes3-18
    s1 = os.path.join(extract_dir, "smali_classes3")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = smali_classes2_dir
    list_package = ["androidx/activity", "androidx/annotation", "androidx/appcompat", "androidx/arch",
                    "androidx/asynclayoutinflater", "androidx/core", "androidx/cursoradapter",
                    "androidx/customview", "androidx/documentfile", "androidx/drawerlayout", "androidx/interpolator"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes5-19
    s1 = os.path.join(extract_dir, "smali_classes5", "androidx")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "androidx")
    list_package = ["fragment", "media", "transition", "vectordrawable", "viewpager"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes8-20
    s1 = os.path.join(extract_dir, "smali_classes8", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["dataeye", "dylanvann", "facebook", "horcrux"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes9-21
    s1 = os.path.join(extract_dir, "smali_classes9", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["mojang", "netease"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes20-22
    s1 = os.path.join(extract_dir, "smali_classes20", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["dataeye", "dylanvann", "facebook", "horcrux"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes21-23
    s1 = os.path.join(extract_dir, "smali_classes21", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["mojang", "netease"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes22-24
    s1 = os.path.join(extract_dir, "smali_classes22", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["dataeye", "dylanvann", "facebook", "horcrux"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes23-24
    s1 = os.path.join(extract_dir, "smali_classes23", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["hw", "masteratul"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes24-26
    s1 = os.path.join(extract_dir, "smali_classes24", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["airbnb", "bumptech", "coolcloud", "dataeye"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes25-27
    s1 = os.path.join(extract_dir, "smali_classes25", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["mojang", "netease"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes4
    s1 = os.path.join(extract_dir, "smali_classes4", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["scwang", "sensors", "shahenlibrary", "sina", "swmansion", "tencent", "yalantis", "yqritc",
                    "yulong", "zmxv"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes17
    s1 = os.path.join(extract_dir, "smali_classes17", "androidx")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "androidx")
    list_package = ["drawerlayout", "loader", "multidex", "slidingpanelayout", "swiperefreshlayout", "versionedparcelable", "viewpager2"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

     # smali_classes18
    s1 = os.path.join(extract_dir, "smali_classes18", "com","facebook")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com","facebook")
    list_package = ["imageformat","imagepipeline","imagepipelinebase","imageutils","infer","jni","nativefilters","nativeimagetranscoder",
                    "perftest","proguard","react","soloader","systrace","webpsupport","widget"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes5
    s1 = os.path.join(extract_dir, "smali_classes5", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["masteratul","mp4parser","noah","oblador","reactnativecommunity"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes5
    s1 = os.path.join(extract_dir, "smali_classes5", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["scwang", "sensors", "shahenlibrary", "sina", "swmansion", "tencent", "yalantis", "yqritc",
                    "yulong", "zmxv"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes6
    s1 = os.path.join(extract_dir, "smali_classes6", "androidx")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "androidx")
    list_package = ["activity","annotation","appcompat","arch","asynclayoutinflater","autofill","core","cursoradapter","customview","documentfile"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes6
    s1 = os.path.join(extract_dir, "smali_classes6", "androidx")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "androidx")
    list_package = ["interpolator","legacy","lifecycle","loader","localbroadcastmanager","media","print"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes6
    s1 = os.path.join(extract_dir, "smali_classes6", "androidx")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "androidx")
    list_package = ["savedstate","tracing","vectordrawable","viewpager2"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes7
    s1 = os.path.join(extract_dir, "smali_classes7", "com")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com")
    list_package = ["facebook"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    # smali_classes8
    s1 = os.path.join(extract_dir, "smali_classes8", "com","netease","ntunisdk")
    smali_classes2_dir = create_mutil_smali(extract_dir)
    s3 = os.path.join(smali_classes2_dir, "com","netease","ntunisdk")
    list_package = ["application","base","coolpad_sdk","external","langutil","ngpush","ngshare"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

def move_package(s1, s3, list_package):
    for d in list_package:
        src = os.path.join(s1, d)
        if os.path.exists(src):
            dst = os.path.join(s3, d)
            distutils.dir_util.copy_tree(src, dst)
            distutils.dir_util.remove_tree(src)


def create_mutil_smali(decompileDir):
    f_idx = 2
    while True:
        tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
        tmp = tmp.replace('\\', '/')
        tmp = re.sub('/+', '/', tmp)

        if os.path.exists(tmp):
            files = os.listdir(tmp)
            if len(files) == 0:
                smali_classes2_dir = tmp
                break
            f_idx += 1
        else:
            smali_classes2_dir = tmp
            break
    # endwhile
    if not os.path.exists(smali_classes2_dir):
        os.mkdir(smali_classes2_dir)
    return smali_classes2_dir


if __name__ == '__main__':
    extract_dir = 'C:/Users/Administrator/Desktop/apk/qmqjbd_8226_bb_2.3.0'
    channel_sdk_info = {'packNameSuffix': "new.package.name"}
    handle_same_package(extract_dir, channel_sdk_info)
