package com.example.system.service.impl;

import com.example.system.mapper.CompetitionMapper;
import com.example.system.mapper.InformationMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.model.InformationTable;
import com.example.system.model.ResultInformationTable;
import com.example.system.model.UserTable;
import com.example.system.repository.InformationRepository;
import com.example.system.service.InformationService;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationRepository informationRepository;
    private UserMapper userMapper;
    @Autowired
    private CompetitionMapper competitionMapper;
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    public InformationServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<InformationTable> getUserInformation(Long userId) {
        return informationRepository.findByUserId(userId);
    }

    @Override
    public InformationTable createInformation(Long userId, Long competitionId, InformationTable information) {
        information.setUserId(userId);
        information.setCompetitionId(competitionId);
        return informationRepository.save(information);
    }

    @Override
    public InformationTable updateInformation(Long informationId, InformationTable information) throws NotFoundException {
        Optional<InformationTable> existingInformationOptional = informationRepository.findById(informationId);
        if (existingInformationOptional.isPresent()) {
            InformationTable existingInformation = existingInformationOptional.get();
            existingInformation.setPersonalInfo(information.getPersonalInfo());
            existingInformation.setRelatedData(information.getRelatedData());
            return informationRepository.save(existingInformation);
        } else {
            throw new NotFoundException("InformationTable with id " + informationId + " not found");
        }
    }

    @Override
    public void deleteInformation(Long informationId) {
        informationRepository.deleteById(informationId);
    }

    //获取所有资料
    @Override
    public List<ResultInformationTable> getAll() {
        List<InformationTable> informationTables = informationRepository.findAll();
        List<ResultInformationTable> ret = new ArrayList<>();
        for (InformationTable table : informationTables) {
            ResultInformationTable tmp = new ResultInformationTable();
            tmp.setProfileId(table.getProfileId());
            tmp.setCompetitionId(table.getCompetitionId());
            tmp.setCompetitionName(competitionMapper.getNameById(tmp.getCompetitionId()));
            tmp.setRelatedData(table.getRelatedData());
            tmp.setPersonalInfo(table.getPersonalInfo());
            tmp.setUserId(table.getUserId());
            UserTable user = userMapper.findUserById(tmp.getUserId());
            tmp.setUserName(user == null ? null : user.getUserName());
            ret.add(tmp);
        }
        return ret;
    }

    //获取指定项目的所有资料
    @Override
    public List<ResultInformationTable> getPortion(Long competitionId) {
        List<ResultInformationTable> tables = informationMapper.getPortion(competitionId);
        for (ResultInformationTable tmp : tables) {
            tmp.setProfileId(tmp.getProfileId());
            tmp.setCompetitionId(tmp.getCompetitionId());
            tmp.setCompetitionName(competitionMapper.getNameById(tmp.getCompetitionId()));
            tmp.setRelatedData(tmp.getRelatedData());
            tmp.setPersonalInfo(tmp.getPersonalInfo());
            tmp.setUserId(tmp.getUserId());
            tmp.setUserName((userMapper.findUserById(tmp.getUserId())).getUserName());
        }
        return tables;
    }

    //获取自己的资料
    @Override
    public List<ResultInformationTable> getSelf(Long userId) {
        List<ResultInformationTable> tables = informationMapper.getSelf(userId);
        for (ResultInformationTable tmp : tables) {
            tmp.setProfileId(tmp.getProfileId());
            tmp.setCompetitionId(tmp.getCompetitionId());
            tmp.setCompetitionName(competitionMapper.getNameById(tmp.getCompetitionId()));
            tmp.setRelatedData(tmp.getRelatedData());
            tmp.setPersonalInfo(tmp.getPersonalInfo());
            tmp.setUserId(tmp.getUserId());
            tmp.setUserName((userMapper.findUserById(tmp.getUserId())).getUserName());
        }
        return tables;
    }
}