package com.izouma.immall.web;

import com.izouma.immall.domain.BackUser;
import com.izouma.immall.domain.User;
import com.izouma.immall.dto.PageQuery;
import com.izouma.immall.exception.BusinessException;
import com.izouma.immall.repo.BackUserRepo;
import com.izouma.immall.security.Authority;
import com.izouma.immall.service.BackUserService;
import com.izouma.immall.utils.ObjUtils;
import com.izouma.immall.utils.SecurityUtils;
import com.izouma.immall.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/back/user")
public class BackUserController extends BaseController {
    private BackUserRepo backUserRepo;
    private BackUserService backUserService;

    @PostMapping("/register")
    public BackUser register(@RequestParam String username,
                         @RequestParam String password) {
        BackUser user = BackUser.builder()
                        .username(username)
                        .nickname(username)
                        .password(new BCryptPasswordEncoder().encode(password))
                        .enabled(true)
                        .authorities(Collections.singleton(new Authority(Authority.NAMES.ROLE_USER.name())))
                        .build();
        return backUserRepo.save(user);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public BackUser save(@RequestBody BackUser user) {
        if (user.getId() != null) {
            BackUser orig = backUserRepo.findById(user.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, user);
            return backUserRepo.save(orig);
        }
        return backUserRepo.save(user);
    }

    @GetMapping("/my")
    public BackUser my() {
        return backUserRepo.findById(SecurityUtils.getAuthenticatedUser().getId()).orElseThrow(new BusinessException("用户不存在"));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<BackUser> all(PageQuery pageQuery) {
        return backUserRepo.findAll(toSpecification(pageQuery, BackUser.class), toPageRequest(pageQuery));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/get/{id}")
    public BackUser get(@PathVariable Long id) {
        return backUserRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

/*    @GetMapping("/back/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<BackUser> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }*/



}
