package com.quinn.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quinn.common.QuinnConstant;
import com.quinn.common.RoleType;
import com.quinn.pojo.*;
import com.quinn.service.*;
import com.quinn.vo.FindNavReq;
import com.quinn.vo.MyPageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author limqsh
 * @since 2022-05-01
 */
@Controller
public class SearchController extends BaseModelController{

    @Resource
    SourceService sourceService;
    @Autowired
    BlogCategoryService blogCategoryService;
    @Autowired
    BlogService blogService;
    @Autowired
    CommentService commentService;
    @Resource
    SourceCategoryService sourceCategoryService;
    @Resource
    FindService findService;
    @Resource
    UserService userService;

    @GetMapping("/search")
    public String searchAll(HttpServletRequest request, String findWhat, Model model){
        /**
         * 为空返回主页
         */
        if (StringUtils.isEmpty(findWhat)){
            return "index";
        }
        String loginUserId = getLoginUserId(request);
        User uid = userService.getOne(new QueryWrapper<User>().eq("uid", loginUserId));
        if (uid != null && RoleType.ADMIN.getName().equals(uid.getRole())){
            String result = doAdmin(findWhat, model);
            if (!QuinnConstant.GUN.equals(result)){
                return result;
            }
        }
        /**
         * 正式开始,全局搜索
         */
        MyPageParam myPageParam = new MyPageParam(1, 20);
        List<FindResult> findList = findService.listFinds(findWhat, myPageParam);
        model.addAttribute("findWhat",findWhat);
        // 结果
        model.addAttribute("findList",findList);
        model.addAttribute("pageParam",myPageParam);
        return "page/allsearch";
    }

    @PostMapping("/search")
    public String search(FindNavReq findNavReq, Model model){
        String findWhat = findNavReq.getFindWhat();
        if (findNavReq.getPageNum() < 1){
            findNavReq.setPageNum(1);
        }
        MyPageParam myPageParam = new MyPageParam(findNavReq.getPageNum(),findNavReq.getLimit());
        List<FindResult> findList = findService.listFinds(findWhat, myPageParam);
        model.addAttribute("findWhat",findWhat);
        // 结果
        model.addAttribute("findList",findList);
        model.addAttribute("pageParam",myPageParam);
        return "page/allsearch::user_table_refresh";
    }

    private String doAdmin(String findWhat,Model model){
        /**
         * 新增资源
         */
        if (QuinnConstant.NEW_SOURCE_PASSWORD.equals(findWhat)){
            List<SourceCategory> categoryList = sourceCategoryService.list(null);
            model.addAttribute("categoryList",categoryList);
            return "source/uploadSource";
        }
        /**
         * 新增公告
         */
        if (QuinnConstant.APPEND_PASSWORD.equals(findWhat)){
            return "page/append";
        }
        /**
         * 修改资源
         */
        if (findWhat.startsWith(QuinnConstant.EDIT_SOURCE_FIRST)){
            String modifyWhat = findWhat.substring(findWhat.indexOf(QuinnConstant.EDIT_SOURCE_FIRST) + QuinnConstant.EDIT_SOURCE_FIRST.length());
            if (!StringUtils.isEmpty(modifyWhat) && modifyWhat.endsWith(QuinnConstant.EDIT_SOURCE_LAST)){
                modifyWhat = modifyWhat.substring(0,modifyWhat.lastIndexOf(QuinnConstant.EDIT_SOURCE_LAST));
            }
            if (!StringUtils.isEmpty(findWhat)){
                Source source = sourceService.getOne(new QueryWrapper<Source>().eq("sid", modifyWhat));
                if (source!=null){
                    model.addAttribute("source",source);
                    // 分类信息
                    List<SourceCategory> categoryList = sourceCategoryService.list(null);
                    model.addAttribute("categoryList",categoryList);
                    return "source/editorSource";
                }
            }
        }
        return QuinnConstant.GUN;
    }


}

