package com.xhkjedu.suser.utils;

import com.xhkjedu.suser.model.system.TUserComponent;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author ywx
 * @className suserUtil
 * @description
 * @date 2022/2/21 15:27
 **/
@Slf4j
public class UserUtil {
    /**
     * 发送form-data请求返回json结果
     *
     * @return java.lang.String
     * @Param [apiUrl, param]
     * @Author ywx
     * @Date 2021/5/14 17:41
     **/
    public static String sendFormData(String apiUrl, MultiValueMap<String, String> param) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            List<MediaType> acceptableMediaTypes = new ArrayList<>();
            acceptableMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
            acceptableMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON_UTF8);
            HttpEntity<MultiValueMap<String, String>> requestParams = new HttpEntity<>(param, headers);
            ResponseEntity<String> response = new RestTemplate().postForEntity(apiUrl, requestParams, String.class);
            String result = response.getBody();
            return result;
        } catch (RestClientException e) {
            log.error("发送form-data请求失败", e.getMessage());
            return null;
        }
    }

    /**
     * 功能描述  校验密码，包含字母和数字，长度6-16
     *
     * @param * @param loginpwd
     * @return ResultVo
     * @author WN
     * @date 2022/3/15
     */
    public static ResultVo checkhLoginPwd(String loginpwd) {
        String passRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,16}$";
        if (loginpwd.matches(passRegex)) {
            return new ResultVo(0, "密码符合规范");
        }

        return new ResultVo(1, "密码必须是8-16位大小写英文字母、数字组合");
    }

    //线程是否执行完
    public static void isAllDone(List<Future> futureList) {
        while (true) {
            if (N_Utils.isListNotEmpty(futureList)) {
                boolean isAllDone = true;
                for (Future future : futureList) {
                    if (null == future || !future.isDone()) {
                        isAllDone = false;
                    }
                }
                if (isAllDone) {
                    break;
                }
            }
        }
    }

    /*
     * @Description 初始化组件
     * @Date 2024/12/9 16:23:01
     * @Author WN
     * @Param []
     * @Return java.util.List<com.xhkjedu.suser.model.system.TUserComponent>
     **/
    public static List<TUserComponent> listInitComponents(){
        List<TUserComponent> initComponents = new ArrayList<>();
        TUserComponent uc1 = new TUserComponent();
        uc1.setCcode("HomeWorkNoticeResearch");
        uc1.setCwidth(1);
        uc1.setCheight(1);
        uc1.setCorder(1);
        initComponents.add(uc1);

        TUserComponent uc2 = new TUserComponent();
        uc2.setCcode("MySpace");
        uc2.setCwidth(1);
        uc2.setCheight(1);
        uc2.setCorder(2);
        initComponents.add(uc2);

        TUserComponent uc3 = new TUserComponent();
        uc3.setCcode("CommonUse");
        uc3.setCwidth(1);
        uc3.setCheight(1);
        uc3.setCorder(2);
        initComponents.add(uc3);

        TUserComponent uc4 = new TUserComponent();
        uc4.setCcode("PreviewHomeworkRecite");
        uc4.setCwidth(2);
        uc4.setCheight(1);
        uc4.setCorder(4);
        initComponents.add(uc4);

        TUserComponent uc5 = new TUserComponent();
        uc5.setCcode("SchoolResourceLibrary");
        uc5.setCwidth(1);
        uc5.setCheight(1);
        uc5.setCorder(5);
        initComponents.add(uc5);

        return initComponents;

    }
}
