package com.purui.prims.headless.server.rest;

import com.github.pagehelper.PageInfo;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.headless.api.pojo.AppAuthConfig;
import com.purui.prims.headless.api.pojo.request.AppQueryReq;
import com.purui.prims.headless.api.pojo.request.AppReq;
import com.purui.prims.headless.api.pojo.response.AppDetailResp;
import com.purui.prims.headless.api.pojo.response.AppResp;
import com.purui.prims.headless.server.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/semantic/app")
@Api(tags = "应用管理")
public class AppController {

    @Autowired
    private AppService appService;

    @ApiOperation(value = "应用创建")
    @PostMapping
    public boolean save(@RequestBody AppReq app,
                        HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        appService.save(app, user);
        return true;
    }

    @ApiOperation(value = "应用授权")
    @PostMapping("/auth/{id}")
    public boolean auth(@PathVariable("id") Long id, @RequestBody List<AppAuthConfig> config,
                        HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        appService.updateConfig(id, config, user);
        return true;
    }

    @PostMapping("/online/{id}")
    public boolean online(@PathVariable("id") Long id,
                          HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        appService.online(id, user);
        return true;
    }

    @PostMapping("/offline/{id}")
    public boolean offline(@PathVariable("id") Long id,
                           HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        appService.offline(id, user);
        return true;
    }

    @ApiOperation(value = "应用删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Long id,
                          HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        appService.delete(id, user);
        return true;
    }

    @ApiOperation(value = "应用详情")
    @GetMapping("/{id}")
    public AppDetailResp getApp(@PathVariable("id") Long id,
                                HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return appService.getApp(id, user);
    }

    @ApiOperation(value = "应用列表")
    @GetMapping("/list")
    public PageInfo<AppResp> pageApp(@RequestBody AppQueryReq appQueryReq,
                                     HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return appService.pageApp(appQueryReq, user);
    }

}
