package com.upsfj.sfjprojectbackend.controller;


import com.upsfj.sfjprojectbackend.entity.*;
import com.upsfj.sfjprojectbackend.service.impl.AlgorithmServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/algorithm")
public class AlgorithmController {
    @Resource
    AlgorithmServiceImpl algorithmService;


    @GetMapping("/findall")
    public List<Algorithm> getAllAlgorithms(){
        List<Algorithm> algorithms = algorithmService.getAllAlgorithms();
        for (Algorithm algorithm:algorithms){
//            System.out.println(algorithm);
//            System.out.println(algorithm.getFieldStr());
            String fieldStr = algorithm.getFieldStr();
            String useStr = algorithm.getUserStr();
            List<String> fieldList = Arrays.asList(fieldStr.split(","));
            List<String> useList = Arrays.asList(useStr.split(","));
            algorithm.setUseList(useList);
            algorithm.setFieldList(fieldList);
        }
//        for (Algorithm algorithm : algorithms){
//            System.out.println(algorithm);
//        }
//        for (Algorithm algorithm : algorithms){
//            List<AlgorithmField> algorithmFields = (List<AlgorithmField>) algorithm.getAlgorithmFields();
//            System.out.println("输出algorithmFields");
//            System.out.println(algorithmFields);
//            String fields = (String) algorithmFields
//                    .stream()
//                    .map(field -> field.getField().toString())
//                    .collect(Collectors.joining(","));
//
////            algorithm.setAlgorithmFields(fields);
//
//            List<AlgorithmUserCase> algorithmUserCases = (List<AlgorithmUserCase>) algorithm.getAlgorithmUserCases();
//            String useCases = algorithmUserCases.stream()
//                    .map(userCase -> userCase.getUsecase().toString())
//                    .collect(Collectors.joining(","));
//
//        }

        return algorithms;
    }

    @GetMapping("/findfield")
    public List<Field> getAllField(){
        return algorithmService.getAllField();
    }


    @GetMapping("/finduse")
    public List<UseCase> getAllUsecase(){
        return algorithmService.getAllUsecase();
    }

    
    @PostMapping("/updateAlgorithm")
    public ResponseEntity<Void> updateAlgorithm(@RequestBody Algorithm algorithm){
        System.out.println("打印接收值");
        System.out.println(algorithm);
        algorithmService.updateAlgorithm(algorithm);


        return ResponseEntity.ok().build();
    }

    @PostMapping("/addAlgorithm")
    public ResponseEntity<Void> addAlgorithm(@RequestBody Algorithm algorithm){
        algorithmService.addAlgorithm(algorithm);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/deleteAlgorithm")
    public ResponseEntity<Void> deleteAlgorithm(@RequestBody Algorithm algorithm){
        algorithmService.deleteAlgorithm(algorithm);
        return ResponseEntity.ok().build();
    }



}
