package com.yutou.tools.nas;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.NasAdminAddressDao;
import com.yutou.tools.mybatis.model.NasAdminAddress;
import com.yutou.tools.mybatis.model.NasAdminAddressExample;
import com.yutou.tools.utils.ConfigTools;
import com.yutou.tools.utils.RedisTools;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
public class NasManager {
    @Resource
    NasAdminAddressDao adminAddressDao;
    @ResponseBody
    @RequestMapping(value = "/auth/nas/address/get.do",method = RequestMethod.GET)
    public String getAdminAddress(HttpServletRequest request){
        JSONObject json=new JSONObject();

        String address= RedisTools.get("adminAddress");
        if(address==null){
            json.put("code",-1);
            json.put("msg","暂未设置管理后台");
        }else{
            JSONObject js=JSONObject.parseObject(address);
            json.put("code",0);
            json.put("msg","当前状态:"+js.getString("url")+":"+js.getString("port"));
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/auth/nas/address/list.do",method = RequestMethod.GET)
    public String addressList(HttpServletRequest request){
        JSONObject json =new JSONObject();
        try{
            List<NasAdminAddress> list=adminAddressDao.selectByExample(new NasAdminAddressExample());
            json.put("code",0);
            json.put("msg","ok");
            json.put("count",list.size());
            if(list.size()==0){
                json.put("data",new JSONArray());
            }else {
                json.put("data", JSONArray.toJSON(list));
            }
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("count",0);
            json.put("msg",e.getMessage());
            json.put("data","[]");
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/auth/nas/address/add.do",method = RequestMethod.POST)
    public String addAddress(HttpServletRequest request,String url,String port,String title) {
        JSONObject json=new JSONObject();
        try {
            NasAdminAddress address=new NasAdminAddress();
            address.setUrl(url);
            address.setPort(Integer.parseInt(port));
            address.setTitle(title);
            adminAddressDao.insert(address);
            json.put("code",0);
            json.put("msg","ok");
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return  json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/auth/nas/address/update.do",method = RequestMethod.POST)
    public String updateAddress(HttpServletRequest request,String url,String port,String title,String id){
        JSONObject json=new JSONObject();
        try {
            NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id));
            if(address!=null) {
                if (!StringUtils.isEmpty(url)) {
                    address.setUrl(url);
                }
                if (!StringUtils.isEmpty(port)) {
                    address.setPort(Integer.parseInt(port));
                }
                if (!StringUtils.isEmpty(title)) {
                    address.setTitle(title);
                }
                adminAddressDao.updateByPrimaryKey(address);
                json.put("code",0);
                json.put("msg","ok");
            }else {
                json.put("code",1);
                json.put("msg","无更新");
            }

        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/auth/nas/address/remove.do",method = RequestMethod.POST)
    public String removeAddress(HttpServletRequest request,String id){
        JSONObject json=new JSONObject();
        try{
            int code=adminAddressDao.deleteByPrimaryKey(Integer.parseInt(id));
            json.put("code",code);
            json.put("msg","ok");
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/auth/nas/address/set.do",method = RequestMethod.POST)
    public String setAddress(HttpServletRequest request,String id){
        JSONObject json=new JSONObject();
        try {
            NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id));
            if(address!=null){
                RedisTools.set("adminAddress",JSONObject.toJSONString(address));
            }
            json.put("code",0);
            json.put("msg","ok");
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/jellyfin.do")
    public void jellyfin(HttpServletResponse response){
        try {
            response.sendRedirect("http://"+UpdateIp.nas_ip+":8096");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/nas/music/getlocalhost.do")
    @ResponseBody
    public String getLocalHost() {
        JSONObject json = new JSONObject();
        json.put("data", "http://" + UpdateIp.nas_ip);
        json.put("code", 0);
        return json.toJSONString();
    }
}
