# -*- coding: utf-8 -*-
# Copyright 2025 AgentUnion Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
from numbers import Number
import time
import requests
import datetime
import requests


from cryptography import x509
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
import datetime
from cryptography.hazmat.primitives.asymmetric import ec

from agentcp.file.wss_binary_message import *
import websocket
from agentcp.base.auth_client import AuthClient
import ssl
import os

from agentcp.utils import tls_config as tls
from agentcp.base.log import log_error, log_exception, log_info

class FileClient:
    def __init__(self, aid_path:str,seed_password:str,agent_id: str, agent_network: str, cert: str = None, key: str = None, agent_id_ref=None):
        """消息客户端类

        Args:
            agent_id: 代理ID
            server_url: 服务器URL
        """
        self.agent_id = agent_id
        self.agent_network = agent_network
        self.server_url = "https://oss." + agent_network + "/api/oss"
        self.cert = cert
        self.key = key
        self._agent_id_ref = agent_id_ref
        self.auth_client = AuthClient(agent_id, self.server_url, aid_path, seed_password)
        self.ws = None
        self.uploaded_url = ""
        # 文件传输读取时间可能较长，适当放宽 read timeout
        self.http_timeout = (10, 600)

    def _get_proxies(self):
        """获取代理配置（agentcp 内部本地/远程一律直连）"""
        return {}
       
    def sign_in(self):
        """登录方法"""
        self.auth_client.sign_in()
    
    def sign_out(self):
        """登出方法""" 
        self.auth_client.sign_out()
    def get_signature(self):
        return self.auth_client.signature

    def close(self):
        if self.ws is not None:
            self.ws.close()


    #标准的post方式上传文件，同步阻塞的方式上传文件，需要修改为异步，并处理各种可能失败的情况
    def post_file(self, full_path):
        try:
            if self.auth_client.signature is None:
                self.sign_in()
            if self.auth_client.signature is None:
                log_error("sign_out failed: signature is None")
                return None
            params = {
                'agent_id': self.agent_id,
                'signature': self.auth_client.signature,
                'file_name': os.path.basename(full_path)
            }
            hb_url = self.server_url + f"/upload_file"
            with open(full_path, 'rb') as file:
                files = {'file': file}
                proxies = self._get_proxies()
                response = requests.post(
                    hb_url,
                    data=params,
                    files=files,
                    verify=tls.requests_verify(),
                    proxies=proxies,
                    timeout=self.http_timeout,
                )
                if response.status_code == 200:
                    log_info("文件上传成功")
                    return response.json()["url"]
                else:
                    log_error(f"文件上传失败，状态码: {response.status_code}")
                    return None
        except FileNotFoundError:
            log_error("文件未找到")
            return None
        except Exception as e:
            log_exception(f"发生错误: {e}")
            return None
        
    def get_uploaded_url(self):
        return self.uploaded_url

    #仅是测试代码，同步阻塞的方式下载文件，需要修改为异步，并处理各种可能失败的情况
    def download_file(self, url, save_path):
        try:
            if self.auth_client.signature is None:
                self.sign_in()
            if self.auth_client.signature is None:
                log_error("sign_out failed: signature is None")
                return None
            hb_url = url + f"?agent_id={self.agent_id}&signature={self.auth_client.signature}"
            try:
                # 发送 GET 请求下载文件
                proxies = self._get_proxies()
                response = requests.get(
                    hb_url,
                    verify=tls.requests_verify(),
                    stream=True,
                    proxies=proxies,
                    timeout=self.http_timeout,
                )
                response.raise_for_status()  # 检查请求是否成功

                # 打开本地文件以写入二进制数据
                with open(save_path, 'wb') as file:
                    # 逐块写入文件，避免一次性加载大文件到内存
                    for chunk in response.iter_content(chunk_size=16384):
                        if chunk:  # 过滤掉保持活动的新块
                            file.write(chunk)

                log_info(f"文件下载成功，保存路径: {save_path}")
                return hb_url,save_path
            except requests.RequestException as e:
                log_error(f"下载文件时发生请求错误: {e}")
                return hb_url,None
            except Exception as e:
                log_exception(f"下载文件时发生未知错误: {e}")
                return hb_url,None
        except Exception as e:
            log_exception(f"download_file in exception: {e}")
        
