import subprocess
import json
import sys
import os


def build(ad_app_id, ad_key, ad_ids, app_display_name,app_bundle_id,app_version,app_icon):
    cmd = """
    rm -rfv build
    mkdir build
    cp -rfv template/PlayBTopOn ./build/
    """

    subprocess.call(cmd, shell=True)
    # 创建编译目录复制模版到build目录

    print("--------------------")
    # 读取配置


    ### 复制icon

    cmd = f"""
    cp -fv {app_icon} ./build/PlayBTopOn/PlayBTopOn/Assets.xcassets/AppIcon.appiconset/h687603756335b.png
    """
    subprocess.call(cmd, shell=True)

    ## 修改ad信息
    print("修改ad信息")
    code_path = './build/PlayBTopOn/PlayBTopOn/playB/bbbAdManager.swift' 
    with open(code_path, 'r') as f:
        code = f.read()
        code = code.replace("{appId}", ad_app_id)
        code = code.replace("{adKey}", ad_key)
        code = code.replace("{allAdIds}", '"' + "\",\"".join(ad_ids) + '"')
    with open(code_path, 'w') as f:
        f.write(code)
        
    print("修改项目信息")
    prj_path = "./build/PlayBTopOn/PlayBTopOn.xcodeproj/project.pbxproj"
    with open(prj_path, 'r') as f:
        code = f.read()
        code = code.replace("{DisplayName}", app_display_name)
        code = code.replace("{BundleId}", app_bundle_id)
        code = code.replace("{Version}", app_version)
    with open(prj_path, 'w') as f:
        f.write(code)

    print("\n开始编译\n")
    cmd = """
    cd ./build/PlayBTopOn
    xcodebuild clean build -workspace PlayBTopOn.xcworkspace -configuration Release -scheme PlayBTopOn  -derivedDataPath "../Target"  -destination "platform=iOS,id=00008110-000815AE1179801E" 
    cd ../../
    """
    subprocess.call(cmd, shell=True)


    print("\n开始打包\n")

    cmd = """
    mkdir ./build/ipas
    cp -rfv ./build/Target/Build/Products/Release-iphoneos/PlayBTopOn.app ./build/ipas/
    cp ./template/embedded.mobileprovision ./build/ipas/PlayBTopOn.app/embedded.mobileprovision
 

    find "./build/ipas/PlayBTopOn.app" -name "*.framework" -exec codesign -f -s "Apple Distribution: Haiyang Tang (544LFS79WN)" {} \;
    find "./build/ipas/PlayBTopOn.app" -name "*.dylib" -exec codesign -f -s "Apple Distribution: Haiyang Tang (544LFS79WN)" {} \;
     
    echo "codesign"
    codesign --entitlements ./template/Filza.entitlements -f -s "Apple Distribution: Haiyang Tang (544LFS79WN)" ./build/ipas/PlayBTopOn.app
    
    mkdir ./build/ipas/Payload
    mv ./build/ipas/PlayBTopOn.app ./build/ipas/Payload
    cd ./build/ipas
    zip -r playb-topon.ipa Payload/
    """

    subprocess.call(cmd, shell=True)
    
    
if __name__ == '__main__':
    
    app_icon = "/Users/mac/codes/ios/empty_topon/ipas/lux/Tikr/appicon.png"
    
    if len(sys.argv) < 2:
        ad_app_id = "h68b560a6957c8"
        ad_key = "a540e4c6e557c1ed3f06b5394246c5a42"
        ad_ids = ["n68b560e6e9866","n68b560e6b3b1f","n68b560e676221","n68b560e6181aa"]

        app_display_name = "WYMT Radar"
        app_bundle_id = "com.wsi.WSIWeatherWYMT-Radar"
        app_version = "5.17.706"
        
        
        build(ad_app_id, ad_key, ad_ids, app_display_name, app_bundle_id, app_version, app_icon)
    else:
        json_path = sys.argv[1]
        with open(json_path, 'r') as f:
            config = json.loads(f.read())
        
        print(config)
       
        ad_app_id = config["sdk_app_id"] 
        ad_key = config["sdk_key"]
        ad_ids = config["ad_ids"]
        app_display_name = config["app_name"]
        app_bundle_id = config["app_pkg_name"]
        app_version = config["app_ver"]
        app_icon_tmp:str = config["app_icon"]
        if not app_icon_tmp is None and app_icon_tmp.strip() != '':
            app_icon = os.path.join(os.path.dirname(json_path),  app_icon_tmp)
        
        build(ad_app_id, ad_key, ad_ids, app_display_name, app_bundle_id, app_version, app_icon)   

