package com.hotent.portal.controller;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.exception.BaseException;
import com.hotent.base.feign.UCFeignService;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.*;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.CommonResultUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.SysIndexLayoutManage;
import com.hotent.portal.model.SysIndexMyLayout;
import com.hotent.portal.params.CacheUserLayoutParam;
import com.hotent.portal.params.IndexLayoutVo;
import com.hotent.portal.persistence.manager.*;
import com.hotent.sys.persistence.manager.SysAuthUserManager;
import com.hotent.sys.persistence.model.SysAuthUser;
import com.hotent.sys.util.SysPropertyUtil;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.api.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/portal/main/v2/")
@Api(tags="门户布局2")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
public class MainPortalController2 {
	@Resource
	SysMessageManager sysMessageManager;
	@Resource
	SysIndexMyLayoutManager sysIndexMylayoutService;
	@Resource
	SysIndexLayoutManageManager sysIndexlayoutManageService;
	@Resource
	SysLayoutSettingManager sysLayoutSettingManager;
	@Resource
	SysLayoutToolsManager sysLayoutToolsManager;
	@Resource
	IUserService ius;
	@Resource
	UCFeignService ucFeignService;
	@Resource
	private SysAuthUserManager sysAuthUserManager;

	/**
	 * 管理端的布局
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="myHome",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "管理端的布局", httpMethod = "GET", notes = "管理端的布局")
	public CommonResult<IndexLayoutVo> myHome() throws Exception {
		CommonResult<IndexLayoutVo> commonResult = sysIndexlayoutManageService.myHome();
		return CommonResultUtil.<IndexLayoutVo>getResult(commonResult);
	}
	/**
	 * 应用端的布局
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="vueFrontHome",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "应用端的布局", httpMethod = "GET", notes = "应用端的布局")
	public CommonResult<IndexLayoutVo> myVueFrontHome() throws Exception {
		CommonResult<IndexLayoutVo> commonResult = sysIndexlayoutManageService.myVueFrontHome();
		return CommonResultUtil.<IndexLayoutVo>getResult(commonResult);

	}
	
	/**
	 * 手机端的布局
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="myMobileHome",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "手机端的布局", httpMethod = "GET", notes = "手机端的布局")
	public CommonResult<IndexLayoutVo> myMobileHome() throws Exception {
		CommonResult<IndexLayoutVo> commonResult = sysIndexlayoutManageService.myMobileHome();
		return CommonResultUtil.<IndexLayoutVo>getResult(commonResult);
	}


	@RequestMapping(value="/appProperties",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取手机app配置", httpMethod = "GET", notes = "获取手机app配置")
	public ResponseEntity<?> appProperties() throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			/*MqttProperty mqttProperty = AppUtil.getBean(MqttProperty.class);
			map.put("host",mqttProperty.getHost());
			map.put("port",mqttProperty.getPort());
			map.put("initServerTime",System.currentTimeMillis());
			map.put("appVersion",SysPropertyUtil.getByAlias("app.version",""));*/
		} catch (Exception e) {
			e.printStackTrace();
			ResponseEntity.badRequest().body(map);
		}
		return ResponseEntity.ok(map);
	}


	@RequestMapping(value="getOnLineCount",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前在线人数", httpMethod = "GET", notes = "获取当前在线人数")
	public CommonResult<String> getOnLineCount(HttpServletRequest httpServletRequest){
		HttpSession session = httpServletRequest.getSession();
        Object count=session.getServletContext().getAttribute("count");
        if(BeanUtils.isEmpty(count)){
        	count = "1";
        }
		return CommonResult.<String>ok().value((String) count);
	}

	@RequestMapping(value="getCurrentUser",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前用户", httpMethod = "GET", notes = "获取当前用户")
	public CommonResult<IUser> getCurrentUser(HttpServletRequest httpServletRequest){
		IUser curUser = ContextUtil.getCurrentUser();
		return CommonResult.<IUser>ok().value(curUser);
	}

	@RequestMapping(value="getImHost",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取im服务配置", httpMethod = "GET", notes = "获取im服务配置")
	public Map<String,Object> getImHost(HttpServletRequest httpServletRequest){
		Map<String,Object> map = new HashMap<String,Object>();
		/*MqttProperty mqttProperty = AppUtil.getBean(MqttProperty.class);
		map.put("host",mqttProperty.getHost());
		map.put("port",mqttProperty.getPort());*/
		map.put("account",ContextUtil.getCurrentUser().getAccount());
		map.put("initServerTime",System.currentTimeMillis());
		return map;
	}
	
	@RequestMapping(value="getAndroidAppVersion",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取安卓app版本号", httpMethod = "GET", notes = "获取安卓app版本号")
	public CommonResult<String> getAndroidAppVersion(){
		String value =  SysPropertyUtil.getByAlias("appVersion", "1.0.0");
		return CommonResult.<String>ok().message("获取app版本号成功！").value(value);
	}
	
	@RequestMapping(value="cacheUserLayout",method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "缓存用户布局设置", httpMethod = "POST", notes = "缓存用户布局设置")
	public CommonResult<String> cacheUserLayout(@ApiParam(name="layout",value="布局数据对象")@RequestBody CacheUserLayoutParam cacheParam){
		IUser user = ContextUtil.getCurrentUser();
		sysIndexlayoutManageService.cacheUserLayout(cacheParam.getLayoutId(), String.valueOf(cacheParam.getLayoutType()), cacheParam.getType(),  user.getUserId());
		return CommonResult.<String>ok().message("缓存成功！");
	}
}
