# -*- coding:utf-8 -*-

"""
服务配置

Author: Gary-Hertel
Date:   2020/07/09
email: <EMAIL>
"""

import json

class __Config:
    """服务配置"""

    def __init__(self):
        pass


    def loads(self, config_file=None):
        """
        加载配置。
        :param config_file:json配置文件
        :return:
        """
        with open(config_file) as json_file:
            configures = json.load(json_file)
        # exchange
        self.access_key = configures['EXCHANGE']['access_key']
        self.secret_key = configures['EXCHANGE']['secret_key']
        self.passphrase = configures['EXCHANGE']['passphrase']
        # push
        self.ding_talk_api = configures['DINGTALK']['ding_talk_api']
        self.accountSID = configures['TWILIO']['accountSID']
        self.authToken = configures['TWILIO']['authToken']
        self.myNumber = configures['TWILIO']['myNumber']
        self.twilio_Number = configures['TWILIO']['twilio_Number']
        self.from_addr = configures['SENDMAIL']['from_addr']
        self.password = configures['SENDMAIL']['password']
        self.to_addr = configures['SENDMAIL']['to_addr']
        self.smtp_server = configures['SENDMAIL']['smtp_server']
        self.mail_port = configures['SENDMAIL']['port']
        self.sendmail = configures['PUSH']['sendmail']
        self.dingtalk = configures['PUSH']['dingtalk']
        self.twilio = configures['PUSH']['twilio']
        # logger
        self.level = configures['LOG']['level']
        self.handler = configures['LOG']['handler']
        # first_run
        self.first_run = configures["STATUS"]["first_run"]
        # ASSISTANT
        price_cancellation_amplitude_str = configures["ASSISTANT"]["amplitude"]
        self.price_cancellation_amplitude = float((price_cancellation_amplitude_str.split("%"))[0]) / 100
        self.time_cancellation = configures["ASSISTANT"]["time_cancellation"]
        self.time_cancellation_seconds = configures["ASSISTANT"]["seconds"]
        self.price_cancellation = configures["ASSISTANT"]["price_cancellation"]
        reissue_order_overprice_range_str = configures["ASSISTANT"]["reissue_order"]
        self.reissue_order = float((reissue_order_overprice_range_str.split("%"))[0]) / 100
        self.automatic_cancellation = configures["ASSISTANT"]["automatic_cancellation"]
        # MONGODB AUTHORIZATION
        self.mongodb_authorization = configures["MONGODB"]["authorization"]
        self.mongodb_user_name = configures["MONGODB"]["user_name"]
        self.mongodb_password = configures["MONGODB"]["password"]
        # MYSQL AUTHORIZATION
        self.mysql_authorization = configures["MYSQL"]["authorization"]
        self.mysql_user_name = configures["MYSQL"]["user_name"]
        self.mysql_password = configures["MYSQL"]["password"]
        # BACKTEST
        self.backtest = configures["MODE"]["backtest"]

    def update_config(self, config_file, config_content):
        """
        更新配置文件
        :param config_file: 配置文件路径及名称，如"config.json"
        :param config_content: 配置文件中的具体字典内容，将以当前content内容替换掉原配置文件中的内容
        :return: 打印"配置文件已更新！"
        """
        with open(config_file, 'w') as json_file:
            json.dump(config_content, json_file, indent=4)
        print("配置文件已更新！")

config = __Config()