package com.yihu.hos.system.controller;

import com.yihu.hos.config.BeanConfig;
import com.yihu.hos.system.model.SystemApp;
import com.yihu.hos.system.model.SystemServiceEndpoint;
import com.yihu.hos.system.service.AppManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 应用管理
 */
@RequestMapping("/app")
@Controller
public class AppController extends BaseController {
    @Resource(name = AppManager.BEAN_ID)
    private AppManager appManager;
    @Autowired
    private BeanConfig beanConfig;

    /**
     *  应用管理管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String appInitial(Model model) {
        model.addAttribute("contentPage", "system/app/app");
        model.addAttribute("dfs",beanConfig.getFsUrl());
        return "partView";
    }

    /**
     * 应用列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getAppList")
    @ResponseBody
    public Result getAppList(HttpServletRequest request,String name,String status) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);
            params.put("status", status);

            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            Result result = appManager.getAppList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 应用-新增/修改页面
     * @param model
     * @param id
     * @param categoryId
     * @return
     */
    @RequestMapping("/editorApp")
    public String editorAppTemplate(Model model, String id, String flag, String categoryId) {
        try {
            SystemApp app = null;
            if (id != null && id.length() > 0) {
                app = appManager.getAppById(id);
            }  else {
                app = new SystemApp();
            }
            model.addAttribute("model", app);
                model.addAttribute("flag", flag);
            model.addAttribute("dfs",beanConfig.getFsUrl());
            model.addAttribute("categoryId", categoryId);
            model.addAttribute("contentPage", "/system/app/editorApp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    /**
     * 应用详情页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/appDetail")
    public String appDetail(Model model, String id) {
        try {
            SystemApp app = null;
            if (id != null && id.length() > 0) {
                app = appManager.getAppById(id);
            }  else {
                app = new SystemApp();
            }
            model.addAttribute("model", app);
            model.addAttribute("contentPage", "/system/app/appDetail");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }


    /**
     * 新增应用信息
     * @param request
     * @return
     */
    @RequestMapping("addApp")
    @ResponseBody
    public Result addApp(HttpServletRequest request) {
        try {
            SystemApp obj = new SystemApp();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setCreateDate(new Date());
            return appManager.addApp(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除应用信息
     * @param request
     * @return
     */
    @RequestMapping("/deleteApp")
    @ResponseBody
    public Result deleteApp(HttpServletRequest request) {

        try {
            String id = request.getParameter("id");
            appManager.deleteApp(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    /**
     * 修改应用信息
     */
    @RequestMapping("updateApp")
    @ResponseBody
    public Result updateApp(HttpServletRequest request) {
        try {
            SystemApp obj = new SystemApp();
            BeanUtils.populate(obj, request.getParameterMap());

            return appManager.updateApp(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    /*  ==================================== 应用服务管理部分 ===================================  */

    @RequestMapping("/initAppService")
    public String AppServiceInit(Model model,String appId) {
        model.addAttribute("appId", appId);
        model.addAttribute("contentPage", "system/app/appService");
        return "partView";
    }

    /**
     * 应用管理
     *   -服务管理页面
     *
     * @param request
     * @return
     */
    @RequestMapping("/getAppServiceList")
    @ResponseBody
    public Result getAppServiceList(HttpServletRequest request,String name,String valid,String appId) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);
            params.put("valid", valid);
            params.put("appId", appId);

            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            Result result = appManager.getAppServiceList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }

    }

    /**
     * 服务修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editorAppService")
    public String editorAppServicePage(Model model, String id,String appId) {
        try {
            SystemServiceEndpoint appService = null;
            if (id != null && id.length() > 0) {
                appService = appManager.getAppServiceById(id);
            }  else {
                appService = new SystemServiceEndpoint();
            }
            model.addAttribute("model", appService);
            model.addAttribute("appId", appId);
            model.addAttribute("contentPage", "/system/app/editorAppService");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    /**
     * 服务详情页
     * @param model
     * @param id
     * @returnServiceS
     */
    @RequestMapping("/appServiceDetail")
    public String appServiceDetail(Model model, String id) {
        try {
            SystemServiceEndpoint app = null;
            if (id != null && id.length() > 0) {
                app = appManager.getAppServiceById(id);
            }  else {
                app = new SystemServiceEndpoint();
            }
            model.addAttribute("model", app);
            model.addAttribute("contentPage", "/system/app/appServiceDetail");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }


    /**
     * 新增服务信息
     * @param request
     * @return
     */
    @RequestMapping("addAppService")
    @ResponseBody
    public Result addAppService(HttpServletRequest request) {
        try {
            SystemServiceEndpoint obj = new SystemServiceEndpoint();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setCreateDate(new Date());
            return appManager.addAppService(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除服务信息
     * @param request
     * @return
     */
    @RequestMapping("/deleteAppService")
    @ResponseBody
    public Result deleteAppService(HttpServletRequest request) {

        try {
            String id = request.getParameter("id");
            appManager.deleteAppService(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    /**
     * 修改服务信息
     */
    @RequestMapping("updateAppService")
    @ResponseBody
    public Result updateAppService(HttpServletRequest request) {
        try {
            SystemServiceEndpoint obj = new SystemServiceEndpoint();
            BeanUtils.populate(obj, request.getParameterMap());

            return appManager.updateAppService(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

}
