package com.reyun.controller;

import com.google.common.collect.Maps;
import com.reyun.model.Account;
import com.reyun.repository.AccountRepository;
import com.reyun.repository.BussinessManRepository;
import com.reyun.security.RedisLoginStatusManager;
import com.reyun.security.TokenManager;
import com.reyun.service.*;
import com.reyun.util.CipherUtil;
import com.reyun.util.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("demo")
public class DemoController {

	protected Logger logger = LoggerFactory.getLogger(DemoController.class);

	@Autowired
	private DemoAppointmentService demoAppointmentService;

	@Autowired
    LoginService loginService;

	@Autowired
    TokenManager tokenManager;

	@Autowired
    AccountRepository accountRepository;

	@Autowired
    UserLogService userLogService;

    @Autowired
    ConfigParamService configParamService;

    @Autowired
    RedisLoginStatusManager redisLoginStatusManager;
    
    @Autowired
    BussinessManRepository bussinessManRepository;
    
    @Autowired
    AuthService authService;
    
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public ResultModel demologin(HttpServletRequest request, HttpServletResponse response) {
		String email = "demo@reyun.com";
		String password = "111111";
		Account account = loginService.login(email, password);
		Map<String, Object> rtnMap = Maps.newHashMap();
		if (account != null) {

            //存储token
			String token = tokenManager.createToken(String.valueOf(account.getId()));
            //存储登陆密码
            redisLoginStatusManager.createLoginStatus(token, account.getIsMasterLogin() ? CipherUtil.generatePassword(configParamService.generateMasterPwd()) : account.getPassword());


            rtnMap.put("code", 200);
            rtnMap.put("token", token);
            rtnMap.put("account", account);
            return ResultModel.OK(rtnMap);

        } else {
            // 没有此账号
            rtnMap.put("code", 205);
            rtnMap.put("msg", "用户名或密码不正确");
            return ResultModel.OK(rtnMap);
        }

    }


}
