using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.ServiceApp.GoView;
using OpenAuth.App.ServiceApp.GoView.Request;
using OpenAuth.App.ServiceApp.GoView.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Domain.GoView;

namespace OpenAuth.WebApi.Controllers.ServiceControllers.GoView;

/// <summary>
///  go view 接口
/// </summary>
[Route("api/goview/project")]
[ApiController]
public class GoViewProjectController : ControllerBase
{
    private readonly GoViewProjectApp _app;

    public GoViewProjectController(GoViewProjectApp goView)
    {
        _app = goView;
    }

    // 文件上传
    [HttpPost("uploadFile")]
    public async Task<Response<bool>> UploadFile(string base64Str)
    {
        return null;
        //return await _app.UploadFile(file);
    }


    /// <summary>
    /// 新增项目
    /// </summary>
    /// <param name="project"></param>
    /// <returns></returns>
    [HttpPost("create")]
    public async Task<Response<GoviewProject>> Create(GoviewProject project)
    {
        return await _app.Create(project);
    }

    /// <summary>
    /// 删除项目
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpPost("delete")]
    public async Task<Response<bool>> DeleteProject(string ids)
    {
        return await _app.DeleteProject(ids);
    }

    /// <summary>
    /// 修改项目
    /// </summary>
    /// <param name="project"></param>
    /// <returns></returns>
    [HttpPost("edit")]
    public async Task<Response<bool>> UpdateProject(GoviewProject project)
    {
        return await _app.UpdateProject(project);
    }


    /// <summary>
    /// 获取项目list集合
    /// </summary>
    ///<param name="state"> -1 1</param>
    /// <param name="page"></param>
    /// <param name="limit"></param>
    /// <returns></returns>
    [HttpGet("list")]
    public async Task<Response<PageInfo<List<GoviewProject>>>> List([FromQuery] GoViewProjectPage req)
    {
        return await _app.List(req);
    }

    /// <summary>
    /// 修改发布状态（发布，取消发布）
    /// </summary>
    /// <param name="id"></param>
    /// <param name="state"></param>
    /// <returns></returns>
    [HttpPost("publish")]
    public async Task<Response<bool>> Publish(GoViewProjectRequest req)
    {
        return await _app.Publish(req);
    }

    /// <summary>
    /// 获取项目数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("getData")]
    public async Task<Response<ProjectResponse>> GetData(string projectId)
    {
        return await _app.GetData(projectId);
    }

    /// <summary>
    /// 保存项目数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("save/data")]
    public async Task<Response<bool>> SaveData(GoviewProjectData data)
    {
        return await _app.SaveData(data);
    }

    #region 组件数据
    /// <summary>
    /// 获取组件集合
    /// </summary>
    /// <param name="page"></param>
    /// <param name="limit"></param>
    /// <returns></returns>
    [HttpGet("getcomponentlist")]
    public async Task<Response<PageInfo<List<GoviewComponent>>>> GetComponentList([FromQuery] GoViewProjectPage req)
    {
        return await _app.GetComponentList(req);
    }
    /// <summary>
    /// 获取组件数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("getComponentData")]
    public async Task<Response<GoviewComponent>> GetComponentData(string componentId)
    {
        return await _app.GetComponentData(componentId);
    }
    /// <summary>
    /// 保存组件数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("saveComponentData")]
    public async Task<Response<bool>> SaveComponentData(GoviewComponent data)
    {
        return await _app.SaveComponentData(data);
    }
    /// <summary>
    /// 删除组件数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("deleteComponentData")] 
    public async Task<Response<bool>> DeleteComponent(string ids)
    {
        return await _app.DeleteComponent(ids);
    }
    #endregion
    /// <summary>
    /// 图片上传接口
    /// </summary>
    /// <param name="files"></param>
    /// <returns></returns>
    [HttpPost("image/upload")]
    [RequestSizeLimit(214748364800)]
    public Response<IList<SysUploadFile>> UploadImage(IFormFileCollection files,string groupName)
    {
        var result = new Response<IList<SysUploadFile>>();
        try
        {
            result.Result = _app.Add(files,groupName);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.Message;
        }

        return result;
    }
    
    // todo 图片分组列表
    [HttpGet("imageGroup/list")]
    public Response<IList<string>> GetImageGroupList()
    {
        return _app.GetImageGroupList();
    }
    
    /// <summary>
    /// 获取组件图片
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet("image/list")]
    public Response<IList<SysUploadFile>> GetImage([FromQuery] GoViewImagePage req)
    {
         
        return new Response<IList<SysUploadFile>>()
        {
            Result = _app.GetImage(req)
        };
    }
    /// <summary>
    /// 删除图片
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpPost("image/delete")]
    public Response<bool> DeleteImage(string ids)
    {
        return new Response<bool>()
        {
            Result = _app.DeleteImage(ids)
        };
    }
    /// <summary>
    /// 取得主页面
    /// </summary>
    /// <returns></returns>
    [HttpGet("mainPage/get")]
    public Response<string> GetMainPage()
    {
        var result = new Response<string>();
        try
        {
            var temp = _app.GetParamByKey("mainPage");
            result.Result = temp;
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.Message;
        }
        return result;
    }
    /// <summary>
    /// 设置主页面
    /// </summary>
    /// <param name="mainPage"></param>
    /// <returns></returns>
    [HttpPost("mainPage/set")]
    public Response<bool> SetMainPage([FromBody] MainPageReq mainPage)
    {
        var result = new Response<bool>();
        try
        {
            result.Result =    _app.AddParam("mainPage", mainPage.MainPage);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.Message;
        }
        return result;
    }
    
    /// <summary>
    /// 自定义组件保存
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("customComponent/save")]
    public async Task<Response<bool>> SaveCustomComponent(CustomComponentReq data)
    {
        var result = new Response<bool>();
        try
        {
            result.Result = await _app.SaveCustomComponent(data);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.Message;
        }
        return result;
    }
    /// <summary>
    ///自定义组件列表
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet("customComponent/list")]
    public async Task<Response<PageInfo<List<GoviewCustomComponent>>>> GetCustomComponentList([FromQuery] PageReq req)
    {
        return await _app.GetCustomComponentList(req);
    }
    
    /// <summary>
    /// 删除自定义组件
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpPost("customComponent/delete")]
    public async Task<Response<bool>> DeleteCustomComponent(string ids)
    {
        
        var result = new Response<bool>();
        if (string.IsNullOrEmpty(ids))
        {
            result.Result = false;
            result.Message = "参数不能为空";
        }
        try
        {
            result.Result = await _app.DeleteCustomComponent(ids);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.Message;
        }
        return result;
    }
    
}