#!/usr/bin/env python
# coding:utf-8

import os
import sys
import time

curr_dir = os.path.split(os.path.abspath(__file__))[0]
print("curr_dir = " + curr_dir)
sys.path.append(os.path.join(curr_dir, "../"))
sys.path.append(os.path.join(curr_dir, "../ipm"))

from ipm.wechat_alert import wechat_alert
import config as config
import gen_levels_db

alert = wechat_alert()
operation = None
platform = None
mode = None
config_country = 'None'
print(sys.argv)
if len(sys.argv) > 1:
    operation = sys.argv[1]
    print(f"operation = {operation}")
if len(sys.argv) > 2:
    platform = sys.argv[2]
    print(f"platform = {platform}")
if len(sys.argv) > 3:
    mode = sys.argv[3]
    print(f"mode = {mode}")
if len(sys.argv) > 4:
    config_country = sys.argv[4]
    print(f"config_country = {config_country}")

if __name__ == '__main__':

    # operation = config.config_pipe_operation.update_main_level_db.value
    # platform = "Android"
    # platform = "iOS"
    # mode = "debug"
    # mode = "release"
    # config_country = '1'

    if operation == config.config_pipe_operation.update_all_levels_google_sheet.value:
        if platform is None:
            exit(1)

        print(f"更新{platform}所有关卡资源表（包含未测试关卡）")
        gen_levels_db.update_all_levels_google_sheet(platform)
        config.notification_helper.append_msg(f"{platform}平台全关卡资源表刷新完成")
        config.notification_helper.append_msg("表格链接： https://docs.google.com/spreadsheets/d/1Hfqjl4Jxg0Eykw1M3WPnoMgnq1lMvo4DXXQELq8CVaI/edit?pli=1&gid=0#gid=0")
    elif operation == config.config_pipe_operation.update_acceptance_passed_sheet.value:
        if platform is None:
            exit(1)

        print(f"更新验收通过资源表")
        gen_levels_db.update_all_acceptance_passed_levels_2_google_sheet(platform)
        time.sleep(5)
        print(f"更新关卡表里对应玩法资源表每一关属性内容")
        gen_levels_db.update_level_config_assets(platform)
        config.notification_helper.append_msg(f"{platform}FindObject验收通过资源表和FindObject可配置关卡资源表刷新完成")
        if platform == "Android":
            config.notification_helper.append_msg(f"验收通过资源表链接： https://docs.google.com/spreadsheets/d/1mwisjfsQwcSqQ0BJ2TszThSZRCRqOaAqgpHaPGGCzYs/edit?pli=1&gid=0#gid=0")
        elif platform == "iOS":
            config.notification_helper.append_msg(f"验收通过资源表链接： https://docs.google.com/spreadsheets/d/1yO142VI6hBldtR_baQMgx4bC3fleYDcULiyb2_OntIY/edit?gid=0#gid=0")
        config.notification_helper.append_msg("可配置关卡资源表格链接： https://docs.google.com/spreadsheets/d/1Y0tkjtgNy90IGSMLdSMFdDPMwP9Wbt0uDSHdyEfHxnA/edit?gid=0#gid=0")
    elif operation == config.config_pipe_operation.update_main_level_db.value:
        if platform is None or mode is None:
            exit(1)

        print(f"更新Main配置表")
        url = gen_levels_db.update_main_level_db(platform, mode)
        config.notification_helper.append_msg(f"{platform}平台的mode:{mode}配置 更新配置文件成功 \n 最新配置文件url: {url}")
        config.notification_helper.append_at_people(config.at_all)
    elif operation == config.config_pipe_operation.update_main_country_level_db.value:
        if platform is None or mode is None:
            exit(1)
        all_country_list = str.split(config_country, ',')
        if config_country != 'None' and len(all_country_list) > 0:
            url_list = gen_levels_db.update_main_country_level_db("Android", "debug", all_country_list)
            for url in url_list:
                config.notification_helper.append_msg(url)
        config.notification_helper.append_at_people(config.at_all)
    else:
        print(f"{operation} 操作类型无法识别!!")
        exit(1)

    config.notification_helper.append_end_msg()
    alert.alert(config.notification_helper.get_msg(), config.notification_helper.get_people_list())