package com.guwan.controller.login;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guwan.common.R;
import com.guwan.config.GlobalValue;
import com.guwan.controller.login.vo.LoginSuccessInfo;
import com.guwan.mapstruct.SimpleDestination;
import com.guwan.mapstruct.SimpleSource;
import com.guwan.mapstruct.SimpleSourceDestinationMapper;
import com.guwan.service.TokenService;
import com.guwan.util.*;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private GlobalValue globalValue;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SimpleSourceDestinationMapper simpleSourceDestinationMapper;

    @Autowired
    private VoiceServiceClient voiceServiceClient;

    @GetMapping("/login")
    @PermitAll
    @Operation(summary = "使用账号密码登录")
    public String login(String userId, HttpServletRequest request) {


        List<User> users = userMapper.selectList(new QueryWrapper<>());
        System.out.println("users = " + users);


        String test = PathUtil.path();


        System.out.println("tempPath = " + test);

        String ip = IPUtils.getIpAddr(request);
        System.out.println("ip111 = " + ip);
       return tokenService.createToken(userId, ip);

    }

    @GetMapping("/getInfo")
    @PermitAll
    @Operation(summary = "查询用户信息")
    public String getInfo(String token) {


        if (!tokenService.checkToken(token)) {
            return "token无效";
        }

        try {
            tokenService.getInfo(token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            tokenService.validateAndExtendToken(token);
        }
        return "getInfo";
    }


    @PostMapping("/demo111")
    public void uploadFile(MultipartFile file) {


        String tempPath = System.getProperty("user.dir") + globalValue.getStaticLocations();

        try {
            // 确保临时路径存在
            File tempDir = new File(tempPath);
            if (!tempDir.exists()) {
                tempDir.mkdirs(); // 创建目录
            }

            // 构建文件的完整路径
            File serverFile = new File(tempPath + File.separator + file.getOriginalFilename());

            // 将上传的文件保存到服务器
            file.transferTo(serverFile);

            System.out.println("文件上传成功，保存路径为：" + serverFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件上传失败：" + e.getMessage());
        }
    }


    @GetMapping("/test")
    public void test1(){
        SimpleDestination sourceName = simpleSourceDestinationMapper.sourceToDestination(new SimpleSource("sourceName", "2"));
        System.out.println("sourceName = " + sourceName);
    }

    @GetMapping("/test123")
    private R test2(String fileUrl){
        System.out.println("fileUrl = " + fileUrl);
        return R.ok().put("data", ToTextExample.voiceToText(fileUrl));
    }


    @GetMapping("/test123456")
    private R test3(String fileUrl){
        System.out.println("fileUrl = " + fileUrl);
        return R.ok().put("data", ToTextExample2.voiceToText(fileUrl));
    }

    @PostMapping("/demo888")
    public JSONObject convertVoiceToText(MultipartFile file, String model) {
        System.out.println(voiceServiceClient.voiceToText(file, model));
        return voiceServiceClient.voiceToText(file, model);
    }

    @GetMapping("/demoLogin")
    public R demo888() {
        System.out.println("tokenService = " + tokenService);
        LoginSuccessInfo loginSuccessInfo = new LoginSuccessInfo();
        loginSuccessInfo.setAvatar("www.baidu.com");
        loginSuccessInfo.setUsername("zhangsan");
        loginSuccessInfo.setNickname("zhangsan");
        ArrayList<String> roles = new ArrayList<>();
        roles.add("Admin");
        loginSuccessInfo.setRoles(roles);
        ArrayList<String> permissions = new ArrayList<>();
        permissions.add("*:*:*");
        loginSuccessInfo.setPermissions(permissions);
        loginSuccessInfo.setAccessToken("123456");
        loginSuccessInfo.setRefreshToken("123456");
        loginSuccessInfo.setExpires("2024-08-09 12:55:32");
        return R.ok().put("data", loginSuccessInfo);


    }



}
