import sys
import os
import io
import shutil

class DeleteUnusedAB:
    def __init__(self, *args):
        
        self.versionFileName = "Version.csv"

        self.hotfixRoots = []
        self.hotfixRoots.append("Hotfix/Android/")
        self.hotfixRoots.append("Hotfix/AndroidLow/")
        self.hotfixRoots.append("Hotfix/AndroidETC2Low/")

        self.hotfixRoots.append("Hotfix/iOS/")
        self.hotfixRoots.append("Hotfix/iOSLow/")

    def clear(self):
        self._iterate_and_clear()

    @staticmethod
    def _read_file_without_bom(file_path):
        content = ""
        if os.path.exists(file_path):
            # 以字符串读取文件
            with io.open(file_path, 'r', encoding="utf-8") as file:
                content = file.read()

        # 判断是否有BOM
        if content[0] == '\ufeff':
            # content(有个BOM)
            content = content[1:]
        return content

    @staticmethod
    def _read_file_lines(file_path):
        content = ""
        if os.path.exists(file_path):
            # 以字符串读取文件
            with io.open(file_path, 'r', encoding="utf-8") as file:
                content = file.readlines()
        return content

    @staticmethod
    def _check_force_updated(path):
        # 读取文件
        contents = ""
        if os.path.exists(path + "/Version.csv"):
            # 以字符串读取文件
            with io.open(path + "/Version.csv", 'r', encoding="utf-8") as file:
                contents = file.readlines()
                # 以,为分隔符，取第二位
                return int(contents[3].split(",")[3]) == 1
        return False

    def _iterate_and_clear(self):
        cur_path = os.getcwd() + "/"
        cur_path = cur_path.replace("\\", "/")
        # 按索引遍历hotfixRoots
        for i in range(len(self.hotfixRoots)):
            hotfix_root = self.hotfixRoots[i]

            if not os.path.exists(hotfix_root):
                continue
            # 遍历hotfixRoot目录下的所有文件夹
            for hotfixLvDir in os.listdir(hotfix_root):
                # 如果字符串hotfixDir包含LV
                if hotfixLvDir.find("LV") != -1:
                    # print(hotfix_root + hotfixLvDir)
                    if self._check_force_updated(hotfix_root + hotfixLvDir):
                        dpath = cur_path + hotfix_root + hotfixLvDir + "/AssetBundle/"
                        print("start Delete: " + dpath)
                        if (os.path.exists(dpath)):
                            shutil.rmtree(dpath)


if __name__ == '__main__':
    DeleteUnusedAB().clear()
