package controllers

import (
	"encoding/json"
	"eta/eta_api/models"
	"eta/eta_api/models/ppt_english"
	"eta/eta_api/services/ppt"
	"strings"
)

type PptEnglishGroupController struct {
	BaseAuthController
}

// GroupList
// @Title 获取可见的目录列表
// @Description 获取可见的目录列表接口
// @Success 200 {object} ppt_english.RespGroupList
// @router /groups [get]
func (this *PptEnglishGroupController) GroupList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	data, err := ppt.GetEnglishGroupsByAdminId(this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
	return
}

// AddGroup
// @Title 新增ppt目录
// @Description 新增ppt目录接口
// @Param	request	body ppt_english.ReqAddPptGroup true "type json string"
// @Success 200 {object} ppt_english.RespAddPptGroup
// @router /group/add [post]
func (this *PptEnglishGroupController) AddGroup() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqAddPptGroup
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupName == "" {
		br.Msg = "请输入目录名称"
		return
	}
	newId, err := ppt.AddEnglishGroup(req.GroupName, this.SysUser.AdminId, 0, 0)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	resp := ppt_english.RespAddPptGroup{
		GroupId: newId,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
	br.Data = resp
}

// DeleteGroup
// @Title 删除ppt目录
// @Description 删除ppt目录接口
// @Param	request	body ppt_english.ReqPptGroup true "type json string"
// @Success 200 Ret=200 删除成功
// @router /group/delete [post]
func (this *PptEnglishGroupController) DeleteGroup() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqPptGroup
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	err = ppt.DelEnglishGroup(req.GroupId, this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
}

// ShareGroup
// @Title 共享/取消共享目录
// @Description 共享/取消共享目录接口
// @Param	request	body ppt_english.ReqPptGroup true "type json string"
// @Success 200 {object} ppt_english.RespPptGroupShare
// @router /group/share [post]
func (this *PptEnglishGroupController) ShareGroup() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqPptGroup
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	data, err := ppt.ShareEnglishGroup(req.GroupId, this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = data
	return
}

// CopyGroup
// @Title 复制目录
// @Description 复制目录接口
// @Param	request	body ppt_english.ReqPptGroup true "type json string"
// @Success 200 Ret=200 复制成功
// @router /group/copy [post]
func (this *PptEnglishGroupController) CopyGroup() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqPptGroup
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	err = ppt.CopyEnglishGroup(req.GroupId, this.SysUser.AdminId, this.SysUser.RealName)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "复制成功"
	return
}

// RenameGroup
// @Title 目录重命名
// @Description 目录重命名接口
// @Param	request	body ppt_english.ReqRenamePptGroup true "type json string"
// @Success 200 Ret=200 重命名成功
// @router /group/rename [post]
func (this *PptEnglishGroupController) RenameGroup() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqRenamePptGroup
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	if req.GroupName == "" {
		br.Msg = "请输入目录名称"
		return
	}
	err = ppt.RenameEnglishGroupName(req.GroupId, req.GroupName, this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "重命名成功"
	return
}

// SharePpt
// @Title 共享/取消共享单个ppt
// @Description 共享/取消共享单个ppt接口
// @Param	request	body ppt_english.ReqPptShare true "type json string"
// @Success 200 {object} ppt_english.RespPptShare
// @router /share [post]
func (this *PptEnglishGroupController) SharePpt() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqPptShareEnglish
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.PptId <= 0 {
		br.Msg = "请输入目录里的ppt序号"
		return
	}
	pptInfo, err := ppt.ShareSingleGroupPptEnglish(req.PptId, this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = ppt_english.RespPptShareEnglish{
		PptId:   pptInfo.PptId,
		IsShare: pptInfo.IsShare,
	}
	return
}

// GroupPptList
// @Title 获取默认的ppt列表或者单个目录内的ppt列表
// @Description 获取可见的目录列表接口
// @Param   GroupId   query   int  true       "目录ID"
// @Success 200 {object} ppt_english.RespGroupPptList
// @router /group/ppt/list [get]
func (this *PptEnglishGroupController) GroupPptList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	groupId, _ := this.GetInt("GroupId")

	data, err := ppt.GetGroupPptEnglishList(int64(groupId), this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		br.IsSendEmail = false
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
	return
}

// MoveGroupPpt
// @Title 移动目录里的ppt
// @Description 移动目录里的ppt接口
// @Param	request	body ppt_english.ReqMoveGroupPpt true "type json string"
// @Success 200 Ret=200 移动成功
// @router /move [post]
func (this *PptEnglishGroupController) MoveGroupPpt() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqMoveGroupPpt
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	err = ppt.MoveGroupPptEnglish(req.GroupId, req.GroupPptId, req.PrevGroupPptId, req.NextGroupPptId, this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "移动成功"
}

// MoveGroup
// @Title 移动目录
// @Description 移动目录接口
// @Param	request	body ppt_english.ReqMoveGroup true "type json string"
// @Success 200 Ret=200 移动成功
// @router /group/move [post]
func (this *PptEnglishGroupController) MoveGroup() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqMoveGroup
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	err = ppt.MoveEnglishGroup(req.GroupId, req.PrevGroupId, req.NextGroupId, this.SysUser.AdminId)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "移动成功"
}

// CopyPpt
// @Title 复制单个ppt
// @Description 复制目录接口
// @Param	request	body ppt_english.ReqCopyPpt true "type json string"
// @Success 200 Ret=200 复制成功
// @router /copy [post]
func (this *PptEnglishGroupController) CopyPpt() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req ppt_english.ReqCopyPpt
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.GroupId <= 0 {
		br.Msg = "请输入目录ID"
		return
	}
	if req.PptId <= 0 {
		br.Msg = "请输入ppt ID"
		return
	}
	data, err := ppt.CopyPptEnglish(req.PptId, req.GroupId, this.SysUser.AdminId, this.SysUser.RealName)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "复制成功"
	br.Data = data
	return
}

// GroupNameList
// @Title 查询目录名称列表
// @Description 查询目录名称列表接口
// @Success 200 {object} ppt_english.RespPptGroupNameList
// @router /group/name [get]
func (this *PptEnglishGroupController) GroupNameList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	list, err := ppt_english.GetPptGroupIdAndGroupNameByAdminId(this.SysUser.AdminId)
	if err != nil {
		br.Msg = "查询目录名称出错" + err.Error()
		return
	}
	data := &ppt_english.RespPptGroupNameList{
		List: list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
	return
}

// SearchGroupPptList
// @Title 按ppt关键词搜索列表
// @Description 按ppt关键词搜索列表接口
// @Param   KeyWord   query   int  true       "搜索关键词"
// @Success 200 {object} ppt_english.RespSearchGroupPptList
// @router /group/ppt/search [get]
func (this *PptEnglishGroupController) SearchGroupPptList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	keyWord := this.GetString("KeyWord")
	data, err := ppt.SearchGroupPptEnglish(keyWord)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
	return
}

// SharePptList
// @Title 获取用户开放的公共Ppt
// @Description 获取用户开放的公共Ppt接口
// @Param   AdminId		query  int		true	"创建人ID"
// @Param   Keyword		query  string	true	"关键字"
// @Param   IsPrivate	query  bool		false	"是否为私有"
// @Success 200 {object} ppt_english.RespGroupPptList
// @router /share/ppt/list [get]
func (this *PptEnglishGroupController) SharePptList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	adminId, _ := this.GetInt("AdminId")
	keyword := this.GetString("Keyword")
	isPrivate, _ := this.GetBool("IsPrivate", false)
	if isPrivate && adminId <= 0 {
		br.Msg = "参数有误, 私有PPT创建人ID不可为空"
		return
	}

	data, err := ppt.GetSharePptEnglishList(adminId, keyword, isPrivate)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
	return
}

// GrantPptList
// @Title 获取用户共享的公共Ppt（分配权限的Ppt）
// @Description 获取用户共享的公共Ppt（分配权限的Ppt）接口
// @Param   GroupId   query   int  true       "分组ID，唉，其实不知道用啥名，就这么用吧"
// @Param   Keyword   query   string  true       "关键字"
// @Success 200 {object} ppt_english.RespGroupPptList
// @router /grant/ppt/list [get]
func (this *PptEnglishGroupController) GrantPptList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	groupId, _ := this.GetInt("GroupId")
	keyword := this.GetString("Keyword")

	var sourceType string
	switch groupId {
	case 1:
		sourceType = "my"
	case 2:
		sourceType = "other"
	default:
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}
	data, err := ppt.GetGrantPptEnglishList(this.SysUser.AdminId, keyword, sourceType)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
	return
}

// ListSearch
// @Title 我的/公开PPT搜索
// @Description 我的/公开PPT搜索
// @Param   Keyword		query  string	true	"关键字"
// @Success 200 {object} ppt_english.RespGroupPptList
// @router /ppt/search [get]
func (this *PptEnglishGroupController) ListSearch() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	keyword := this.GetString("Keyword")
	keyword = strings.TrimSpace(keyword)

	data, err := ppt.SearchEnglishPptList(sysUser.AdminId, keyword)
	if err != nil {
		br.Msg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = data
}
