package cn.com.qmth.examcloud.tool.service;

import cn.com.qmth.examcloud.tool.cache.LoginSessionManager;
import cn.com.qmth.examcloud.tool.config.SysProperty;
import cn.com.qmth.examcloud.tool.service.export_student_answer_and_score_detail.vo.CourseVO;
import cn.com.qmth.examcloud.tool.utils.HttpHelper;
import cn.com.qmth.examcloud.tool.utils.JsonMapper;
import cn.com.qmth.examcloud.tool.vo.Pager;
import cn.com.qmth.examcloud.tool.vo.user.User;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CommonService {

    private final static Logger log = LoggerFactory.getLogger(CommonService.class);

    @Autowired
    private SysProperty sysProperty;

    public User login(String serverUrl, String loginName, String password, String smsCode) {
        // serverUrl 示例：abc.exam-cloud.cn
        String[] values = serverUrl.split("\\.");
        String domain = values[0] + ".ecs.qmth.com.cn";
        String httpUrl = "https://" + serverUrl;
        log.info("serverUrl:{}  domain:{}", httpUrl, domain);

        Map<String, Object> params = new HashMap<>();
        params.put("domain", domain);
        params.put("accountType", "COMMON_LOGIN_NAME");
        params.put("accountValue", loginName);
        params.put("password", password);
        if (StringUtils.isNotBlank(smsCode)) {
            params.put("smsCode", smsCode);
        }

        return this.doLogin(httpUrl, params);
    }

    private User doLogin(String serverUrl, Map<String, Object> params) {
        String url = serverUrl + "/api/ecs_core/auth/login";
        String result = HttpHelper.post(url, null, params);
        log.info("LOGIN_IN {}", result);

        User user = new JsonMapper().parseJson(result, User.class);
        user.setServerUrl(serverUrl);

        LoginSessionManager.addLoginSession(user);
        return user;
    }

    /**
     * 获取考试相关的课程列表
     */
    public List<CourseVO> getExamCourseList(User user, Long examId) {
        Map<String, Object> params = new HashMap<>();
        params.put("examId", examId);
        params.put("pageNo", 1);
        params.put("pageSize", 1000);

        Map<String, String> headers = new HashMap<>();
        headers.put("key", user.getKey());
        headers.put("token", user.getToken());

        String url = user.getServerUrl() + "/api/ecs_exam_work/exam/course/list";
        String json = HttpHelper.post(url, headers, params);

        JsonMapper jsonMapper = new JsonMapper();
        Pager<CourseVO> page = jsonMapper.parseJson(json, new TypeReference<Pager<CourseVO>>() {

        });

        if (page != null && page.getContent() != null) {
            return page.getContent();
        }

        return new ArrayList<>();
    }

}
