package com.cxy.classroll.student.service.impl;

import com.cxy.classroll.common.execption.CxyException;
import com.cxy.classroll.common.result.Result;
import com.cxy.classroll.common.result.ResultCodeEnum;
import com.cxy.classroll.model.entity.User;
import com.cxy.classroll.model.form.AuthenticationForm;
import com.cxy.classroll.model.vo.IdentifyAndID;
import com.cxy.classroll.student.client.StudentFeignClient;
import com.cxy.classroll.student.service.CommonWebService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class CommonWebServiceImpl implements CommonWebService {

    @Autowired
    private StudentFeignClient client;

    @Autowired
    private StudentFeignClient studentFeignClient;

    @Override
    public IdentifyAndID login(String code) {
        Result<IdentifyAndID> loginResult = client.login(code);
        Integer codeResult = loginResult.getCode();
        if (codeResult!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }

        IdentifyAndID data = loginResult.getData();
        Long id = data.getId();
        if (id == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return data;
    }

    @Override
    public Boolean authentication(AuthenticationForm authenticationForm) {
        Result<Boolean> result = studentFeignClient.authentication(authenticationForm);
        if (result.getData()){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User getUserInfo(Long id) {
        Result<User> result = client.getUserInfo(id);
        Integer code = result.getCode();
        if (code!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        User data = result.getData();
        if (data == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }

        return data;
    }

    @Override
    public Boolean studentSignInClass(Long user_id, Long lesson_id) {
        Result<Boolean> result = studentFeignClient.StudentSignInClass(user_id, lesson_id);
        Integer code = result.getCode();
        if (code!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        Boolean data = result.getData();
        if (data == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return data;
    }

    @Override
    public Boolean studentSignOutClass(Long user_id, Long lesson_id) {
        Result<Boolean> result = studentFeignClient.StudentSignOutClass(user_id, lesson_id);
        Integer code = result.getCode();
        if (code!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        Boolean data = result.getData();
        if (data == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return data;
    }

    @Override
    public Integer listenLessonStatus(Long lesson_id) {
        Result<Integer> result = studentFeignClient.LessonStatus(lesson_id);
        if (result.getCode()!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        if (result.getData() == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return result.getData();
    }

    @Override
    public Integer listenLessonRoll(Long lesson_id) {
        Result<Integer> result = studentFeignClient.LessonRoll(lesson_id);
        if (result.getCode()!=200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        if (result.getData() ==null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return result.getData();
    }

    @Override
    public String classRoll(Long lesson_id, int roll_method) {
        Result<String> result = studentFeignClient.classRoll(lesson_id, roll_method);
        if (result.getCode() != 200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        if (result.getData() ==null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return result.getData();
    }

    @Override
    public List<User> scoreRank() {
        Result<List<User>> result = studentFeignClient.scoreRank();
        if (result.getCode() != 200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        if (result.getData() == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return result.getData();
    }

    @Override
    public Boolean teacherComment(Long lesosn_id, String student_no, int score) {
        Result<Boolean> result = studentFeignClient.comment(lesosn_id, student_no, score);
        if (result.getCode() != 200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        if (result.getData() == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return result.getData();
    }

    @Override
    public List<String> leesonStudentNoList(Long lesson_id) {
        Result<List<String>> result = studentFeignClient.getNoList(lesson_id);
        if (result.getCode() != 200){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        if (result.getData() == null){
            throw new CxyException(ResultCodeEnum.DATA_ERROR);
        }
        return result.getData();
    }
}
