/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.youchain.appupdate.rest;

import com.youchain.Netty.NettyUtils;
import com.youchain.Netty.ServerListenerHandler;
import com.youchain.annotation.Log;
import com.youchain.annotation.rest.AnonymousGetMapping;
import com.youchain.appupdate.domain.SysAppUpdate;
import com.youchain.appupdate.service.SysAppUpdateService;
import com.youchain.appupdate.service.dto.SysAppUpdateQueryCriteria;
import com.youchain.appupdate.service.dto.SysNewAppUpdateDto;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author liuxue
* @date 2023-07-17
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "APP版本管理")
@RequestMapping("/api/sysAppUpdate")
public class SysAppUpdateController {

    private final SysAppUpdateService sysAppUpdateService;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysAppUpdate:list')")
    public void exportSysAppUpdate(HttpServletResponse response, SysAppUpdateQueryCriteria criteria) throws IOException {
        sysAppUpdateService.download(sysAppUpdateService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("app_update")
    @PreAuthorize("@el.check('sysAppUpdate:list')")
    public ResponseEntity<Object> querySysAppUpdate(SysAppUpdateQueryCriteria criteria, Pageable pageable){

        return new ResponseEntity<>(sysAppUpdateService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @ApiOperation("获取最新版本tdshfdisuhfosihfosi093t")
    @AnonymousGetMapping(value = "/new")
    public ResponseEntity<Object> querySysNewAppUpdate(SysAppUpdateQueryCriteria criteria, Pageable pageable){

        log.error(criteria.getBaseurl());
        List<SysNewAppUpdateDto> list= sysAppUpdateService.queryByname(criteria);
        if(list.size()>0){
            SysNewAppUpdateDto so=list.get(list.size()-1);

            String url=so.getDownloadUrl();
            so.setCode("0");
            so.setUpdateStatus(1);
            so.setDownloadUrl(criteria.getBaseurl()+url);
            return new ResponseEntity<>(so,HttpStatus.OK);
        }

        return new ResponseEntity<>("",HttpStatus.OK);
    }


    @PostMapping
    @ApiOperation("上传文件")
    @PreAuthorize("@el.check('sysAppUpdate:add')")
    public ResponseEntity<Object> createSysAppUpdate(@RequestParam String appName,@RequestParam int versionCode,@RequestParam String versionName,@RequestParam String modifyContent, @RequestParam("file") MultipartFile file){

        return new ResponseEntity<>(sysAppUpdateService.create(appName,versionCode,versionName,modifyContent,file),HttpStatus.CREATED);

    }



    @PutMapping
    @Log("修改app_update")
    @ApiOperation("修改app_update")
    @PreAuthorize("@el.check('sysAppUpdate:edit')")
    public ResponseEntity<Object> updateSysAppUpdate(@Validated @RequestBody SysAppUpdate resources){
        sysAppUpdateService.update(resources);

        for (Channel client : ServerListenerHandler.clients) {
            String devce_ip =((InetSocketAddress)client.remoteAddress()).getAddress().getHostAddress();
            NettyUtils.light_on(devce_ip,NettyUtils.light_1,null);
        }

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除app_update")
    @ApiOperation("删除app_update")
    @PreAuthorize("@el.check('sysAppUpdate:del')")
    public ResponseEntity<Object> deleteSysAppUpdate(@RequestBody Integer[] ids) {
        sysAppUpdateService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
