package com.yutou.nas.Controllers;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.nas.utils.ConfigTools;
import com.yutou.nas.utils.RedisTools;
import com.yutou.nas.utils.StringUtils;
import com.yutou.nas.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Objects;

@Controller
public class DoorController {
    @RequestMapping("/door/data.do")
    @ResponseBody
    public JSONObject getData(){
        JSONObject json=new JSONObject();
        String zoom=RedisTools.get("door_zoom");
        if(StringUtils.isEmpty(zoom)){
            zoom="0";
        }
        json.put("zoom", Float.parseFloat(zoom));
        json.put("audio",RedisTools.get("door_audio"));
        json.put("restart",RedisTools.get("door_reboot"));
        json.put("af",RedisTools.get("door_af"));
        File file=new File("web"+File.separator+"apk"+File.separator+"door");
        if(file.exists()&& Objects.requireNonNull(file.listFiles()).length!=0){
            JSONObject update=new JSONObject();
            update.put("versionName", Objects.requireNonNull(file.listFiles())[0].getName().replace("app-release-","").replace(".apk",""));
            update.put("url","http://"+ ConfigTools.load(ConfigTools.CONFIG,"nas_ip",String.class)+":803/apk/door/"+ Objects.requireNonNull(file.listFiles())[0].getName());
            json.put("update",update);
        }
        RedisTools.remove("door_audio");
        RedisTools.remove("door_reboot");
        RedisTools.remove("door_af");
        return json;
    }
    @RequestMapping("/door/zoom.do")
    @ResponseBody
    public String setZoom(float zoom){
        if(zoom>1){
            zoom=1;
        }
        if(zoom<0){
            zoom=0;
        }
        RedisTools.set("door_zoom",zoom+"");
        return "1";
    }
    @RequestMapping(value="/door/audio.do")
    @ResponseBody
    public String setAudio(@RequestParam("audio") MultipartFile audio){
        System.out.println("收到音频");
        try {
            String path=Tools.createFile("audio",audio,System.currentTimeMillis()+".mp3");
            RedisTools.set("door_audio","http://"+ConfigTools.load(ConfigTools.CONFIG,"nas_ip",String.class)+":801/"+path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "1";
    }
    @RequestMapping(value="/door/audio_url.do")
    @ResponseBody
    public String setAudioUrl(String url){
        RedisTools.set("door_audio",url);
        return "1";
    }
    @RequestMapping("/door/reboot.do")
    @ResponseBody
    public String reboot(){
        RedisTools.set("door_reboot","1");
        return "1";
    }
    @RequestMapping("/door/af.do")
    @ResponseBody
    public String af(){
        RedisTools.set("door_af","1");
        return "1";
    }
}
