package com.yihu.hos.remotemanage.controller;

import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.interceptor.LocalContext;
import com.yihu.hos.remotemanage.service.RemoteShellService;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 远程终端管理--shell操作
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/1/5.
 */
@RequestMapping("/shell")
@Controller
public class RemoteShellController extends BaseController {

    @Resource(name = RemoteShellService.BEAN_ID)
    private RemoteShellService remoteShellService;


    /**
     * 远程shell操作页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String appInitial(Model model) {
        model.addAttribute("contentPage", "/monitor/shell/shell");
        return "partView";
    }

    @RequestMapping(value = "/sendShell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Result sendShell(
            @ApiParam(name = "command", value = "shell命令")
            @RequestParam(value = "command", required = false) String command,
            @ApiParam(name = "disCon", value = "是否断开会话连接")
            @RequestParam(value = "disCon", required = true) boolean disCon) {
        String result = "";
        try {
            //发送shell命令 消息
            remoteShellService.sendShell(command, disCon);
            return Result.success("shell请求发送成功:"+command);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("shell请求发送失败！");
    }


    @RequestMapping(value = "/getShellBack", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String getShellBack() {
        //TODO  切换用户的时候记得清除该值
        String result = "";
        try {
            //TODO 如何去除等待时间，目前添加sleep是因为需要等待zbus回调方法的返回结果；
            String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
            result = LocalContext.getContext().getAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
            int count = 0;
            while (result == null) {
                count++;
                Thread.sleep(1000);
                result = LocalContext.getContext().getAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
                //获取失败时，尝试再一次获取结果
                if (count > 5) {
                    break;
                }
            }
            LocalContext.getContext().removeAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
            System.out.println("接口返回结果：" + result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return result;
    }


}
