#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os

from utils.include_utils import auto_parse_include
from utils.other_utils import get_file_suffix, extract_codes_colors, replace_old_color, update_themes, \
    replace_res_utils, bg_white_radius10, bg_btn_ffffff_10dp, add_xml_res_auto, \
    add_import_R, bg_homeindex_item_10, unify_path_separator, \
    get_included_suffix, get_excluded_files, update_deprecated, bg_btn_white_topr10, \
    bg_btn_white_topr12, bg_btn_white_topr16, replace_xml_imageview, replace_xml_textview, \
    replace_system_bar, delete_xml_remark, replace_icon, replace_background, hykb_dimens


# 替换java/kotlin/xml文件中的颜色硬编码
def replace_file_by_path(file_path: str, auto_include_callback=None):
    # 整理文件路径
    file_path = unify_path_separator(file_path)
    if not os.path.isfile(file_path):
        print(f"{file_path} 不是一个文件")
        return
    # 获取文件后缀
    suffix = get_file_suffix(file_path)
    if suffix not in get_included_suffix():
        print(f"忽略{suffix}格式文件")
        return
    # 获取文件名
    file_name = os.path.basename(file_path)
    if file_name in get_excluded_files():
        print(f"{file_name} 是忽略文件")
        return
    print(f"处理文件:{file_path}")

    with open(file_path, 'r', encoding='utf-8') as f:
        text = f.read()

    text = text.replace("\"#ffffffff\"", "\"#ffffff\"")
    text = text.replace("\"#FFFFFFFF\"", "\"#ffffff\"")

    # 自动解析include
    auto_parse_include(text, suffix, auto_include_callback)
    # fix过时方法
    text = update_deprecated(text)
    # 提取文件中的颜色硬编码
    text = extract_codes_colors(text, suffix)
    # 替换旧的颜色
    text = replace_old_color(text)
    # 更新主题
    text = update_themes(text)
    # 删除xml注释
    text = delete_xml_remark(text)
    # 替换旧的dimen
    text = hykb_dimens(text)

    text = bg_white_radius10(text)
    text = bg_homeindex_item_10(text)
    text = bg_btn_ffffff_10dp(text)
    # text = MoreButtonStyle(text)
    # text = BaseTextOneLineStyle(text)

    text = bg_btn_white_topr10(text)
    text = bg_btn_white_topr12(text)
    text = bg_btn_white_topr16(text)

    text = replace_xml_imageview(text)
    text = replace_xml_textview(text)
    text = replace_icon(text)
    text = replace_background(text)

    # 错误校正,非必须
    text = text.replace("green_brandDark", "green_brand")
    text = text.replace("green_brand_brand", "green_brand")
    text = text.replace("green_brand_brandDark", "green_brand")
    text = text.replace("green_brandDark", "green_brand")
    text = text.replace("green_brand_08", "green_brand_7")
    text = text.replace("green_brand_brand_word", "green_word")
    text = text.replace("green_brand_word", "green_word")
    text = text.replace("green_brand_hover", "green_hover")
    text = text.replace("green_brand_line", "green_line")
    text = text.replace("green_brand_e0fce7", "green_e0fce7")
    text = text.replace("bg_light_00", "transparent")
    text = text.replace("font_color_ff131715", "black_h1")
    text = text.replace("green_brand_bg", "green_bg")
    text = text.replace("color_ff131715", "black_h1")
    text = text.replace("color_green_", "green_brand_")

    # text = text.replace("Color.parseColor(\"#ffffff\")", "ResUtils.getColor(R.color.white)")
    # text = text.replace("Color.parseColor(\"#ffffffff\")", "ResUtils.getColor(R.color.white)")
    # text = text.replace("android:textColor=\"#ffffffff\"", "android:textColor=\"@color/white\"")
    # text = text.replace("android:background=\"#ffffffff\"", "android:background=\"@color/bg_white\"")
    # text = text.replace("setImageDrawable(ContextCompat.getDrawable(mActivity, ", "setImageResource((")

    # 添加xml头信息
    text = add_xml_res_auto(text)
    # 替换旧方法
    text = replace_res_utils(text, suffix)
    # 添加R文件引入
    text = add_import_R(text, suffix)
    # 删除Java多余空白行
    # text = delete_java_blank_line(text)
    # 删除xml多余空白行
    # text = delete_xml_blank_line(text, suffix)

    text = text.replace("color_333333", "black_h2")
    text = text.replace("color_999999", "black_h4")

    # 处理状态栏
    text = replace_system_bar(text)

    # 替换旧文件中的内容
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(text)