#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2026. Huawei Technologies Co.,Ltd. All rights reserved.
#
# 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 os
import platform
import re
import time

from tests.st.lib.common.Ssh import ClassSsh
from tests.st.lib.common.Ssh import STD_OUT


class ClassCLI(ClassSsh):

    def __init__(self, ip, username, password):
        super(ClassCLI, self).__init__(ip, username, password)
        self.wait_str = "root@"

    def execute_command(self, command, timeout=300, waitstr=None, path=None, inputList=None):
        inputList = inputList if inputList else []
        command_dict = {
            "command": [command],
            "timeout": timeout,
            "input": inputList
        }
        if path:
            command_dict["directory"] = path

        result = self.cmd(command_dict)
        if not result:
            return result

        # Regex stripping is mostly unnecessary with exec_command, but we keep basic strip
        if isinstance(result.get('stdout'), str):
            result['stdout'] = result['stdout'].strip()
        return result

    def get_cmd_result(self):
        command = {"command": ["echo $?"]}
        ret = self.cmd(command)
        return ret[STD_OUT]

    def wait_for_reboot(self, ip_addr=None, wait_time=600, timout=20, pack=3, type_par=""):
        os_type = platform.system()
        if not ip_addr:
            ip_addr = self.ip
        if os_type == "Linux":
            cmd_str = "ping {} -c {}".format(ip_addr, pack)
        elif os_type == "Windows":
            raise Exception("wrong os")
        startTime = time.time()
        endTime = startTime + wait_time
        count = 1
        while time.time() <= endTime:
            self.logger.info("ping {} ,wait time {}".format(ip_addr, str(time.time() - startTime)))
            ping_info = os.popen(cmd_str)
            ping_list = list(filter(lambda line: "ms" in line.lower() and "ttl" in line.lower(),
                                    ping_info.readlines()))
            if len(ping_list) >= int(pack):
                self.logger.info("{} connected".format(ip_addr))
                break
            else:
                count += 1
                time.sleep(5)
        else:
            self.logger.warning("out of time connected")
            return False
        for _ in range(5):
            result = self.login(type_par)
            if result:
                self.logger.info("login {} success".format(ip_addr))
                res = self.syn_system_time()
                if res:
                    self.logger.info("Success in synchronizing time")
                else:
                    self.logger.warning("failed to synchronizing time")
        else:
            self.logger.warning("login {} failed".format(ip_addr))
            return False

    def syn_system_time(self):
        modify_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        res = self.cmd({"command": ["date", "-s", "'" + modify_time + "'"]})
        from tests.st.lib.common.Ssh import RESULT_CODE
        return res[RESULT_CODE] == 0

    def view_file_directory(self, folder, timeout=60, wait_str=""):
        command = {"command": ["ll %s| awk '{print$9}'" % folder],
                   "wait_str": wait_str,
                   "timeout": timeout}
        ret = self.cmd(command)
        if "No Such file or directory" not in ret['stdout']:
            ls_file_lists = ret.get('stdout').split('\r\n')
            self.logger.info("all the files in the folder are %s" % ls_file_lists)
            return ls_file_lists
        else:
            self.logger.warning("not find the folder")
            return None

    def delete_dir_path(self, dir_f, wait_str=""):
        command = {'command': [f"rm -rf {dir_f} && [ ! -d {dir_f} ]"]}
        ret = self.cmd(command)
        if ret.get('rc') == 0:
            self.logger.warning("delete success")
            return True
        else:
            self.logger.warning("delete failed")
            return None

    def get_wait_str(self):
        return self.wait_str