package com.hotent.portal.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.hotent.base.annotation.NewVersion;
import com.hotent.portal.service.MainPortalService;
import org.springframework.beans.factory.annotation.Autowired;
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 com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
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.SysIndexLayoutManageManager;
import com.hotent.portal.persistence.manager.SysIndexMyLayoutManager;
import com.hotent.sys.persistence.manager.SysAuthUserManager;
import com.hotent.sys.util.SysPropertyUtil;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/portal/main/v1/")
@Api(tags="门户布局")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
@NewVersion
public class MainPortalController  {
	@Resource
	SysIndexMyLayoutManager sysIndexMylayoutService;
	@Resource
	SysIndexLayoutManageManager sysIndexlayoutManageService;
	@Autowired
	private MainPortalService mainPortalService;

	/**
	 * 管理端的布局
	 * @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 {
		return mainPortalService.myHome();
	}
	/**
	 * 应用端的布局
	 * @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 {
		return mainPortalService.myVueFrontHome();
	}

	/**
	 * 手机端的布局
	 * @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 {
		return mainPortalService.myMobileHome();
	}


	@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 String getOnLineCount(HttpServletRequest httpServletRequest){
		HttpSession session = httpServletRequest.getSession();
        Object count=session.getServletContext().getAttribute("count");
        if(BeanUtils.isEmpty(count)){
        	count = "1";
        }
        return (String) count;
	}

	@RequestMapping(value="getCurrentUser",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前用户", httpMethod = "GET", notes = "获取当前用户")
	public IUser getCurrentUser(HttpServletRequest httpServletRequest){
		IUser curUser = ContextUtil.getCurrentUser();
		return 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 new CommonResult<String>(true, "获取app版本号成功！", 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 new CommonResult<String>(true, "缓存成功！");
	}
}
