package com.goafanti.common.utils;

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

import org.thymeleaf.expression.Lists;

import com.goafanti.demand.bo.DemandListBo;

public class LimitLengthUtil {
	
	//科技需求首页
	public static void limiteDemandLength(List<DemandListBo> list,int titleLength,int descripLength) {
		for(DemandListBo demandListBo:list) {
			demandListBo.setName(limitLengthAndAddPoints(demandListBo.getName(),titleLength ));
			demandListBo.setProblemDes(limitLengthAndAddPoints(demandListBo.getProblemDes(), descripLength));
		}
	}
	public static void limiteDemandCompanyAndName(List<DemandListBo> list,int titleLength,int descripLength) {
		for(DemandListBo demandListBo:list) {
			demandListBo.setName(limitLengthAndAddPoints(demandListBo.getName(),titleLength ));
			demandListBo.setEmployerName(limitLengthAndAddPoints(toConceal(demandListBo.getEmployerName()), descripLength));
		}
	}

	public static String limitLengthAndAddPoints(String s,int l) {
		
		if(s!=null) {
			if(s.length()>l) {
				String reString=s.substring(0, l);
				StringBuilder stringBuilder=new StringBuilder(reString);
				stringBuilder.append("...");
				return stringBuilder.toString();
				
			}
			else return s;
			
		}
		return s;
	}
	
	//脱敏
	private static final int SIZE = 8;
    private static final String SYMBOL = "*";

    @SuppressWarnings("unused")
	public static String toConceal(String value) {
        if (null == value || "".equals(value)) {
            return value;
        }
        int len = value.length();
        int pamaone = len / 2;
        int pamatwo = pamaone - 1;
        int pamathree = len % 2;
        StringBuilder stringBuilder = new StringBuilder();
        if (len <= 2) {
            if (pamathree == 1) {
                return SYMBOL;
            }
            stringBuilder.append(SYMBOL);
            stringBuilder.append(value.charAt(len - 1));
        } else {
            if (pamatwo <= 0) {
                stringBuilder.append(value.substring(0, 1));
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.substring(len - 1, len));

            } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
                int pamafive = (len - SIZE) / 2;
                stringBuilder.append(value.substring(0, pamafive));
                for (int i = 0; i < SIZE; i++) {
                    stringBuilder.append(SYMBOL);
                }
                if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
                    stringBuilder.append(value.substring(len - pamafive, len));
                } else {
                    stringBuilder.append(value.substring(len - (pamafive + 1), len));
                }
            } else {
                int pamafour = len - 2;
                stringBuilder.append(value.substring(0, 1));
                for (int i = 0; i < pamafour; i++) {
                    stringBuilder.append(SYMBOL);
                }
                stringBuilder.append(value.substring(len - 1, len));
            }
        }
        return stringBuilder.toString();

    }
    
    /*
     * 随机列表
     * list-原始列表
     * count 返回长度
     * queryLength 查询长度
     * return list
     * 
     * */
 public static  <T>List<T>  LimitListLengthRandomly(List<T> list,int count,int queryLength)
 {
	 int truelyQueryLength=Math.min(list.size(), queryLength);
	 count=Math.min(count, truelyQueryLength);
	 if(list==null || list.size()<=1)return list;
	 if(list==null || list.size()<=count)return list;
	 else {
		 int[] randomInts=RandomUtil.generateRandomList(truelyQueryLength, count);
		 List<T>res=new ArrayList<>();
		 for(int i=0;i<count;i++) {
			 res.add(list.get(randomInts[i]));
		 }
		 return res;
	 }
	 
 }

}
