package me.qwq.doghouse.controller.admin;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import me.qwq.doghouse.controller.common.BaseController;
import me.qwq.doghouse.entity.User;
import me.qwq.doghouse.exception.RException;
import me.qwq.doghouse.pojo.dto.R;
import me.qwq.doghouse.service.UserService;
import me.qwq.doghouse.util.CookieUtils;
import me.qwq.doghouse.util.Cryptos;
import me.qwq.doghouse.util.SessionUtils;

@Controller
@RequestMapping("/admin/v2")
public class AdminController extends BaseController {

    @Autowired
    UserService adminUserService;
    
    @GetMapping({"", "/", "/index"})
    public String index() {
        return "admin/index";
    }
    
    @GetMapping("/getCurrentUserInfo")
    @ResponseBody
    public R<?> getCurrentUserInfo() {
        User user = SessionUtils.getUser(session);
        user.setPassword(null);
        return R.ok(user);
    }
    
    static final String EMPTY_PASSWORD = Cryptos.sha3("", 224);
    
    static boolean passwordIsNotEmpty(String password) {
        return StringUtils.isNotEmpty(password) &&
                !password.equalsIgnoreCase(EMPTY_PASSWORD);
    }
    

    @PostMapping("/setCurrentUserInfo")
    @ResponseBody
    public R<?> setCurrentUserInfo(@Validated User changedUser, BindingResult bindingResult, String newPassword) {
        
        if (bindingResult.hasErrors()) {
            throw new RException(HttpStatus.BAD_REQUEST, bindingResult.getAllErrors().get(0).getDefaultMessage())
            .setLogRequest(true);
        }
        
        User user = SessionUtils.getUser(session);
        boolean hasNewPass = passwordIsNotEmpty(newPassword);
        if (hasNewPass && !user.getPassword().equals(changedUser.getPassword())) {
            throw RException.badRequest("旧密码不正确");
        }
        
        LambdaUpdateWrapper<User> uw = new LambdaUpdateWrapper<User>();
        
        uw.set(User::getEmail, changedUser.getEmail())
          .set(User::getNickname, changedUser.getNickname())
          .set(User::getSite, changedUser.getSite());
          
        if (hasNewPass) {
            uw.set(User::getPassword, newPassword);
        }
        
        if (adminUserService.update(uw)) {
            user = adminUserService.getById(user.getUserId());
            SessionUtils.setUser(session, user);
            CookieUtils.setUserToCookie(response, user);
            return R.ok(user.setPassword(null));
        }
        throw RException.internalServerError();
    }

}
