package com.lightinit.hsdataportal.controller.share;

import com.lightinit.hsdataportal.controller.BaseController;
import com.lightinit.hsdataportal.entity.Admin;
import com.lightinit.hsdataportal.entity.User;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IUserVerifyService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.crypto.hash.Md5Hash;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;

/**
 * @Author: sunxiang
 * @Date: 2018/6/27 14:28
 * @Description:
 **/
@Controller
@RequestMapping(value = "/share/userverify")
public class ShareUserVerifyController extends BaseController{

    @Autowired
    private IUserVerifyService userVerifyService;
    @RequiresAuthentication
    @RequestMapping(value="detail.htm")
    public ModelAndView detail(HttpSession session) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView=new ModelAndView("share/user_verify/baseInformation");//未认证基本页面
        UserPersonalModel model = userVerifyService.getAdminBaseInforamtion(loginUser.getId());
        modelAndView.addObject("outputModel",model);
        return modelAndView;
    }

    @RequiresAuthentication
    @RequestMapping(value="changePassword.action")
    @ResponseBody
    public ResultState changePassword(String oldpassword,String newpassword,String renewpassword,HttpSession session) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResultState resultState = new ResultState();
        Admin admin= userVerifyService.getAdmin(loginUser.getId());
        if(admin ==null){
           resultState.setData("用户不存在！");
            return resultState;
        }
        if(StringUtils.isEmpty(oldpassword) || StringUtils.isEmpty(newpassword)){
            LinkedHashMap<Object, Object> hashMap = new LinkedHashMap<>();
            hashMap.put("oldpassword","密码不能为空");
            hashMap.put("newpassword","密码不能为空");
            return buildInvalidResultState("",hashMap);
        }
        if(StringUtils.isNotEmpty(renewpassword) && StringUtils.isNotEmpty(newpassword) && !(newpassword.equals(renewpassword))){
            LinkedHashMap<Object, Object> hashMap = new LinkedHashMap<>();
            hashMap.put("newpassword","两次密码输入不正确");
            hashMap.put("renewpassword","两次密码输入不正确");
            return buildInvalidResultState("",hashMap);
        }
        if(! admin.getPassword().equals(new Md5Hash(oldpassword, admin.getUsername() + admin.getSalt(), 3).toHex())){
            LinkedHashMap<Object, Object> hashMap = new LinkedHashMap<>();
            hashMap.put("oldpassword","旧密码不正确");
            return buildInvalidResultState("",hashMap);
        }
        admin.setPassword(new Md5Hash(newpassword, admin.getUsername() + admin.getSalt(), 3).toHex());
        return userVerifyService.changeAdminPassword(admin);
    }
}
