package com.xunmei.common.core.utils;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.xunmei.common.core.utils.snowId.DateHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cn.hutool.core.date.DatePattern.PURE_DATETIME_PATTERN;

@Component
public class IDHelper {


    //    @Value("${snowflake.workerId}")
//    private  long workerId;
//    @Value("${snowflake.datacenterId}")
//    private long datacenterId=0L;
//    private  static  long _workerId=0L;
//    private  static  long _datacenterId=0L;
    @Autowired
    private Environment environment;

//    @PostConstruct
//    public void setStatic() {
//        long _workerId = 0L;
//        long _datacenterId = 0L;
//        if (environment.containsProperty("snowflake.workerId")) {
//            _workerId = environment.getProperty("snowflake.workerId", long.class);
//        }
//        if (environment.containsProperty("snowflake.datacenterId")) {
//            _datacenterId = environment.getProperty("snowflake.datacenterId", long.class);
//        }
//
////        snowflaskGenerator = new SnowflaskGenerator(_workerId, _datacenterId);
//        snowflaskGenerator = new SnowflaskGenerator(0L, 0L);
//    }

//    private static  SnowflaskGenerator snowflaskGenerator = null;

    private static DefaultIdentifierGenerator idGenerator=new   DefaultIdentifierGenerator();
    public static Long id() {
        return idGenerator.nextId(null);
    }

    public static String dateTime(final Date date) {
        return DateUtil.format(date, PURE_DATETIME_PATTERN);
    }

    public static String dateTime(final Date date, final Serializable other) {
        return DateUtil.format(date, PURE_DATETIME_PATTERN) + "|" + other.toString();
    }

    public static String date(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "D" + dateHelper.getMonth() + "" + dateHelper.getDate() + "|" + other.toString();
    }

    public static String month(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "M" + dateHelper.getMonth() + "|" + other.toString();
    }

    public static String week(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "W" + dateHelper.getWeek() + "|" + other.toString();
    }

    public static String quarter(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "Q" + dateHelper.getQuarter() + "|" + other.toString();
    }

    public static String halfyear(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "H" + dateHelper.getHalfyear() + "|" + other.toString();
    }

    public static String year(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "Y" + dateHelper.getYear() + "|" + other.toString();
    }

    public static String hour(final Date date, final Serializable other) {
        final DateHelper dateHelper = new DateHelper(date);
        return dateHelper.getYear() + "R" + dateHelper.getMonth() + "" + dateHelper.getDate() + "" + dateHelper.getHour() + "|" + other.toString();
    }

    public static Long pad(final Serializable code, final Serializable id, final int i) {
        return Long.parseLong(code + StringUtils.leftPad(id.toString(), i, "0"));
    }

    public static Long and(final Object... pp) {
        return Long.parseLong(Stream.of(pp)
                .filter(Objects::nonNull)
                .map(Object::toString)
                .collect(Collectors.joining()));
    }

    public static String md5(final Serializable... os) {
        final StringBuilder stringBuilder = new StringBuilder(os.length * 10);
        for (final Serializable o : os) {
            stringBuilder.append(o.toString());
        }
        stringBuilder.trimToSize();
        return DigestUtils.md5Hex(stringBuilder.toString());
    }
}
