package thyyxxk.webserver.controller.settings;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.PersonnelQueryConditions;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.settings.deptphones.DeptPhones;
import thyyxxk.webserver.entity.settings.permissions.DeptTree;
import thyyxxk.webserver.entity.settings.permissions.Role;
import thyyxxk.webserver.entity.settings.permissions.SaveRoleMenusParam;
import thyyxxk.webserver.entity.settings.permissions.SaveUserRolesParam;
import thyyxxk.webserver.entity.settings.users.ChangePwdParam;
import thyyxxk.webserver.entity.settings.users.WorkIntegrationPlatformAdvice;
import thyyxxk.webserver.service.settings.SettingsService;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/settings")
public class SettingsController {
    private final SettingsService service;

    @Autowired
    public SettingsController(SettingsService service) {
        this.service = service;
    }

    @GetMapping("/getUserInfo")
    public ResultVo<UserInfo> getUserInfo() {
        return service.getUserInfo();
    }

    @PostMapping("/changePassword")
    public ResultVo<String> changePassword(@RequestBody ChangePwdParam param) {
        return service.changePassword(param);
    }

    @GetMapping("/getDeptTree")
    public ResultVo<List<DeptTree>> getDeptTree() {
        return service.getDeptTree();
    }

    @PostMapping("/getAllUsers")
    public ResultVo<IPage<UserInfo>> getAllUsers(@RequestBody PersonnelQueryConditions param) {
        return service.getAllUsers(param);
    }

    @GetMapping("/getAllRoles")
    public ResultVo<List<Role>> getAllRoles() {
        return service.getAllRoles();
    }

    @GetMapping("/getUserRoles")
    public ResultVo<List<Integer>> getUserRoles(@RequestParam("code") String code) {
        return service.getUserRoles(code);
    }

    @PostMapping("/saveUserRoles")
    public ResultVo<String> saveUserRoles(@RequestBody SaveUserRolesParam param) {
        return service.saveUserRoles(param);
    }

    @GetMapping("/addNewRole")
    public ResultVo<String> addNewRole(@RequestParam("name") String name) {
        return service.addNewRole(name.trim());
    }

    @GetMapping("/getDeptPhones")
    public ResultVo<List<DeptPhones>> getDeptPhones() {
        return service.getDeptPhones();
    }

    @PostMapping("/saveDeptPhone")
    public ResultVo<String> saveDeptPhone(@RequestBody DeptPhones param) {
        return service.saveDeptPhone(param);
    }

    @GetMapping("/getMyAdvices")
    public ResultVo<List<WorkIntegrationPlatformAdvice>> getMyAdvices() {
        return service.getMyAdvices();
    }

    @PostMapping("/submitNewAdvice")
    public ResultVo<String> submitNewAdvice(@RequestBody WorkIntegrationPlatformAdvice advice) {
        return service.submitNewAdvice(advice);
    }

    @GetMapping("/checkAdvice")
    public ResultVo<String> checkAdvice(@RequestParam("id") Integer id) {
        return service.checkAdvice(id);
    }

    @GetMapping("/dismissUserBadge")
    public ResultVo<String> dismissUserBadge(@RequestParam("id") Integer id) {
        return service.dismissUserBadge(id);
    }

    @PostMapping("/replyAdvice")
    public ResultVo<String> replyAdvice(@RequestBody WorkIntegrationPlatformAdvice advice) {
        return service.replyAdvice(advice);
    }

    @GetMapping("/getRoleVueMenus")
    public ResultVo<List<Integer>> getRoleVueMenus(@RequestParam("id") Integer id) {
        return service.getRoleVueMenus(id);
    }

    @PostMapping("/saveRoleVueMenus")
    public ResultVo<String> saveRoleVueMenus(@RequestBody SaveRoleMenusParam param) {
        return service.saveRoleVueMenus(param);
    }

    @PostMapping("/modifyEmployeeInfo")
    public ResultVo<String> modifyEmployeeInfo(@RequestBody UserInfo userInfo) {
        return service.modifyEmployeeInfo(userInfo);
    }

    @PassToken
    @PostMapping("/saveEmployeeInfo")
    public ResultVo<String> saveEmployeeInfo(@RequestBody UserInfo userInfo) {
        return service.saveEmployeeInfo(userInfo);
    }

    @Data
    public static class ResetPassword {
        private String code;
        private Integer nextTime;
    }

    @PostMapping("/resetPasswordByCode")
    public ResultVo<String> resetPasswordByCode(@RequestBody ResetPassword r) {
        return service.resetPasswordByCode(r.getCode(), r.getNextTime());
    }

    @GetMapping("/selectAutographImageByCode")
    public ResultVo<Map<String, Object>> selectAutographImageByCode(@RequestParam("code") String code) {
        return service.selectAutographImageByCode(code);
    }

    @PostMapping("/putAutographImage")
    public ResultVo<JSONObject> putAutographImage(@RequestBody MultipartFile file,
                                                  @RequestParam(value = "code", required = false) String code) {
        return service.putAutographImage(file, code);
    }

    @PostMapping("/setUserConfig")
    public ResultVo<String> setUserConfig(@RequestBody UserInfo js) {
        service.setUserConfig(js.getUserConfig());
        return ResultVoUtil.success();
    }

    @PassToken
    @GetMapping("/importUser")
    public void importUser() throws IOException {
        service.importUser();
    }
}
