package cn.teammodel;

import cn.teammodel.common.FiveEducations;
import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.service.impl.EvaluationServiceImpl;
import cn.teammodel.utils.SchoolDateUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author winter
 * @create 2023-11-10 10:42
 */
@Slf4j
public class TestWithoutSpring {
    @Test
    public void testJwtDecode() {
        // 解析 JWT，不验证签
        //        Object roles = JWTUtil.parseToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlR4OFo1d2lkT2VSdDVCSGdWX2lvdXpzbjd6OCJ9.eyJhdWQiOiI3MjY0MzcwNC1iMmU3LTRiMjYtYjg4MS1iZDU4NjVlN2E3YTUiLCJpc3MiOiJodHRwczovL2xvZ2luLnBhcnRuZXIubWljcm9zb2Z0b25saW5lLmNuLzQ4MDdlOWNmLTg3YjgtNDE3NC1hYTViLWU3NjQ5N2Q3MzkyYi92Mi4wIiwiaWF0IjoxNzEwNzQ0MzA4LCJuYmYiOjE3MTA3NDQzMDgsImV4cCI6MTcxMDc0ODIwOCwiYWlvIjoiNDJaZ1lPQS9KL0t0ZTIzdWFzN2dEMGFPdW80WHpmdjlmMXhUREp6ZlZsNzk0VWkwWndVQSIsImF6cCI6ImM3MzE3Zjg4LTdjZWEtNGU0OC1hYzU3LWExNjA3MWY3Yjg4NCIsImF6cGFjciI6IjEiLCJvaWQiOiJkNDliYjc4Mi02ZGMyLTQ5MzktODY3OC1kMzNjMjY3MTliZjkiLCJyaCI6IjAuREFJQXota0hTTGlIZEVHcVctZGtsOWM1S3dRM1pITG5zaVpMdUlHOVdHWG5wNlVCQUFBLiIsInJvbGVzIjpbIklFUyJdLCJzdWIiOiJkNDliYjc4Mi02ZGMyLTQ5MzktODY3OC1kMzNjMjY3MTliZjkiLCJ0aWQiOiI0ODA3ZTljZi04N2I4LTQxNzQtYWE1Yi1lNzY0OTdkNzM5MmIiLCJ1dGkiOiJ6MGhKdDg0c1UwMlpxNTJNdGNRS0FBIiwidmVyIjoiMi4wIn0.U9KWPccJQ4ZgfzHBNTK6swuEJvkZM8ebfrOpAl7uDoNynlVEsp8CcQ2a1PIO3V8X8tcNassCplc08qbCqcL49R3S5vP2bWTiucT7c_fyCogxMYpmRM0RmGz8RF1tFF3VAxG4_q1Gt9LgzLbNhxxCQcbgJuZhUMKAP246esHlJrkue88FwgmMxf_rwxDH8VwbAchHtSuIRbj3zYSzgKU0Lpmibrr88g_gQZ061f4RyRezTmKLoOqhoGjEosOOnXOdI67YyxpdMjm64uzc92fNUyC9PHvu8XbTTbdUlkU2lPgrk5xFfyVus9SdxWQXB4PCIDhcBsoVQQlWGmY-kvw_8w").getPayload("roles");
        //        System.out.println(roles);

        Claims claims = null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(new SecretKeySpec("fXO6ko/qyXeYrkecPeKdgXnuLXf9vMEtnBC9OB3s+aA=".getBytes(StandardCharsets.UTF_8), "HmacSHA256"))
                    .setAllowedClockSkewSeconds(315360000)
                    .parseClaimsJws("eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ3d3cud2ludGVhY2guY24iLCJzdWIiOiI1NTUzMzMwNzQ1OTU4NzY4NjQiLCJleHAiOiIxNzExNjE1MDIxIiwibmFtZSI6IuWRqOWTjeWGmyIsInBpY3R1cmUiOm51bGwsImNkanhqeSI6eyJuaWNrbmFtZSI6IuWRqOWTjeWGmyIsInBpY3R1cmUiOm51bGwsImJpbmRJZCI6IjE4NDgyMTMzMDk0Iiwib3BlbmlkIjpudWxsLCJ1bmlvbmlkIjoiMWE4ZWFkNDYtY2JkMS00Y2VlLTg2NzEtZTU0NmMwZmQ4NTMyIn19._6ltuihVcWopQQqfgpSLo3aMVKE3MJhb28NQBSL_w7s")
                    .getBody();
            System.out.println(claims);
        } catch (Exception e) {
            log.warn("token 解析出错:{}",e.getMessage());
        }
    }

    @Test
    public void testDingDing() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=32d9b24f69c2c4fd7c2dab43268b6258a7214d2620e0805d7b6d1429003b64b6");
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent("测试告警文本消息");
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(Arrays.asList("15196506772"));
// isAtAll类型如果不为Boolean，请升级至最新SDK
        at.setIsAtAll(true);
        at.setAtUserIds(Arrays.asList("109929","32099"));
        request.setAt(at);

        request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl("https://www.dingtalk.com/");
        link.setPicUrl("");
        link.setTitle("告警时代的火车向前开");
        link.setText("告警这个即将发布的新版本，创始人xx称它为红树林。而在此之前，每当面临重大升级，产品经理们都会取一个应景的代号，这一次，为什么是红树林");
        request.setLink(link);

        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("杭州天气");
        markdown.setText("#### 告警杭州天气 @156xxxx8827\n" +
                "> 9度，西北风1级，空气良89，相对温度73%\n\n" +
                "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n"  +
                "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
        request.setMarkdown(markdown);
        OapiRobotSendResponse response = client.execute(request);
    }

    @Test
    public void testException() {
        try {
            throw new Exception("出现异常");
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("error: {}", e);
            //System.out.println(e.getMessage());
            //System.out.println(e.getCause());
            //System.out.println(e.getStackTrace());
        }
    }
    @Test
    public void testResult() {
        EvaluationServiceImpl service = new EvaluationServiceImpl();
        Appraise appraise = new Appraise();
        appraise.setCode("evaluation");
        appraise.setPeriodId("default");
        List<AppraiseTreeNode> nodes = new ArrayList<>();

        String root2Id = UUID.randomUUID().toString();
        AppraiseTreeNode root1 = new AppraiseTreeNode();
        root1.setId(root2Id);
        root1.setPid(null);
        root1.setName("root1");
        root1.setScore(0);
        // 1
        String rootId = UUID.randomUUID().toString();
        AppraiseTreeNode node = new AppraiseTreeNode();
        node.setId(rootId);
        node.setPid(null);
        node.setName("root");
        node.setScore(0);


        // 2
        AppraiseTreeNode node1 = new AppraiseTreeNode();
        String subId = UUID.randomUUID().toString();
        node1.setId(subId);
        node1.setPid(rootId);
        node1.setName("child-1");
        node1.setScore(0);
        // 3
        AppraiseTreeNode node2 = new AppraiseTreeNode();
        node2.setId(UUID.randomUUID().toString());
        node2.setPid(subId);
        node2.setName("child-2");
        node2.setScore(0);
        nodes.add(node);
        nodes.add(root1);
        nodes.add(node1);
        nodes.add(node2);
        appraise.setNodes(nodes);

        //List<EvaluationTreeNode> tree = service.buildTree(nodes);
        //System.out.println(JSONUtil.parse(service.buildTree(tree)).toStringPretty());
        //
        //List<EvaluationTreeNode> nodeList = new ArrayList<>();
        //service.flattenTree(tree, nodeList);
        //System.out.println(JSONUtil.parse(nodeList).toStringPretty());
    }

    @Test
    public void testLocalDateTime() {
        List<School.Semester> semesters = Arrays.asList(
                new School.Semester("17f85c96-253d-4f84-84ad-c819f0b605s2", "下学期", 0, 2, 17), // 2024-3   , 正确: 2023-3
//                new School.Semester("2", "下学期", 0, 5, 1),
                new School.Semester("08b81e76-e7d2-4001-8b4c-e7c789ef4bs1", "上学期", 1, 8, 1)
//                new School.Semester("4", "下学期", 0, 11, 1)
        );

        LocalDate currentDate = LocalDate.of(2024, 2, 21);

//        String id = SchoolDateUtil.calculateAcademicYearId(semesters, currentDate);
        System.out.println(SchoolDateUtil.getSemesterByNow(semesters, currentDate));
    }

    @Test
    public void testWeek() {
        LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochMilli(1690819200000L), ZoneOffset.of("+08:00"));
        LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochMilli(1708099200000L),ZoneOffset.of("+08:00"));
        long l = SchoolDateUtil.calculateWeekNum(start, end, 1702025230000L);
        System.out.println(l);
//        LocalDateTime now = LocalDateTime.now();
//        LocalDateTime time = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
//                .withHour(0)
//                .withMinute(0)
//                .withSecond(0)
//                .withNano(0);
//
//        LocalDateTime time1 = LocalDateTime.of(2023, 12, 18, 0, 0);
//        System.out.println(ChronoUnit.WEEKS.between(time, time1) + 1);

    }

    @Test
    public void testDate() {
        Integer a = 1000;
        int b = 1000;
        // 包装类碰到基本类型,会自动拆箱,这时候用 == 没事
//        System.out.println(a == b);

        System.out.println(FiveEducations.codes());
    }
}
