# /*
#  * Copyright (C) 2020, KylinSoft Co., Ltd.
#  *
#  * This program is free software: you can redistribute it and/or modify
#  * it under the terms of the GNU General Public License as published by
#  * the Free Software Foundation, either version 3 of the License, or
#  * (at your option) any later version.
#  *
#  * This program is distributed in the hope that it will be useful,
#  * but WITHOUT ANY WARRANTY; without even the implied warranty of
#  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  * GNU General Public License for more details.
#  *
#  * You should have received a copy of the GNU General Public License
#  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
#  */

#!/usr/bin/python3

import logging
import json
from gi.repository import Gio
from SystemUpdater.modules.i18n import _
from SystemUpdater.Core.errors import *
from SystemUpdater.Core.enums import *
from dataclasses import dataclass, asdict,field
from SystemUpdater.Core.DataAcquisition import create_time_stamp
from typing import Optional, List, Dict
from typing import List, Dict, Set, Optional,Union,Tuple
from pydantic import BaseModel

class PostAction(BaseModel):
    command: Optional[str] = None   # 可执行文件路径
    value: Optional[str] = ""       # 附加信息（来源目录/分类）
    priority: int = 100             # 优先级（由文件名前缀数字决定）
    success: bool = True
    error_desc: Optional[str] = None

@dataclass
class DeployState:
    distro: str = None
    deployed_refs: str = None
    revision: str = None
    rollback_deployment: list = field(default_factory=list)
    timestamp: str = None
    def asdict(self):
        self.timestamp = create_time_stamp()[0:-4]
        return asdict(self)

class Transaction(BaseModel):
    success: bool = False
    sender: str = None
    is_kylin: bool = False
    from_revision: str = None
    new_revision: str = None
    status: str = None
    available_refs: str = None
    deployed_refs: str = None
    available_repo: str = None
    download_limit: int = 0
    hooks: Dict[str, List[PostAction]] = {}
    timestamp: str = None
    error: str = None
    error_message: str = None
    push_list: list = []
    role: str = ROLE_UNSET
    update_metadata: dict = {}

    # 标识部署完成后 是重启还是关机
    reboot_required: bool = True

    # 表示前端是否已经读取当前的更新状态 读取后会将此值设置为True
    has_read: bool = False

    def cancelled():
        logging.info("Task has been cancelled...")
    _cancellable = Gio.Cancellable.new()
    _cancellable.connect(cancelled)

    def asdict(self):
        return json.loads(self.json())