using Microsoft.AspNetCore.Mvc;
using NetPanel.Bl;
using NetPanel.Entity;
using NetPanel.Help;

namespace NetPanel.Controllers.Home
{
    public class OverviewController : Controller
    {
        /// <summary>
        /// 获取概况
        /// </summary>
        /// <returns></returns>
        public IActionResult GetOverview()
        {
            ReturnMsg returnMsg = new ReturnMsg();

            try
            {
                if (SystemHelp.IsLinux())
                {

                    #region 内存
                    string output = ExecuteBl.Exec("free", "-m");

                    // 解析输出行
                    string[] lines = output.Split('\n');
                    string[] memoryInfo = lines[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    // 获取已使用和总内存大小
                    float totalMemory = float.Parse(memoryInfo[1]);
                    float usedMemory = float.Parse(memoryInfo[2]);

                    LogHeler.SelErrt("[HomeController-GetOverview]" + output);

                    #endregion

                    #region 存储
                    output = ExecuteBl.Exec("df", "-h ./");
                    lines = output.Split('\n');
                    memoryInfo = lines[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    // 转换为GB单位
                    double totalSizeInGB = memoryInfo[1].Replace("G", "").ToDouble();
                    double availableFreeSpaceInGB = memoryInfo[2].Replace("G", "").ToDouble();

                    #endregion

                    #region Cpu使用率
                    output = ExecuteBl.Exec("/usr/bin/top", "-b -n 1");
                    string cpuLine = output.Split('\n')[2];
                    string[] cpuValues = cpuLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    double cpuUsage = double.Parse(cpuValues[1]);

                    #endregion

                    var data = new
                    {
                        //cpu
                        Cpu = new { Total = 100, Used = cpuUsage },
                        //内存
                        Ram = new { Total = totalMemory, Used = usedMemory },
                        Size = new { Total = totalSizeInGB, Used = availableFreeSpaceInGB },
                    };
                }
                else
                {
                    var data = new
                    {
                        //cpu
                        Cpu = new { Total = 100, Used = new Random().Next(0, 100) },
                        //内存
                        Ram = new { Total = 1000, Used = new Random().Next(0, 1000) },
                        //内存
                        Size = new { Total = 2000, Used = new Random().Next(0, 2000) },
                    };
                    returnMsg.Obj = data;
                }
                returnMsg.Code = 1;

            }
            catch (Exception es)
            {
                LogHeler.SelErrt("[HomeController-GetOverview]" + es.ToString());
            }
            return Ok(returnMsg);
        }

    }
}
