package com.dcsoft.system.utils;

import cn.hutool.core.util.NumberUtil;
import com.dcsoft.common.core.constant.Constants;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class FloorUtil {

    public static String floorConvert(List<String> storeyInfoVos) {
        int j = 0;
        for (String storeyInfoVo : storeyInfoVos) {
            if ("19A".equals(storeyInfoVo)) {
                storeyInfoVos.set(j, "18");
            }
            j++;
        }
        String storey = storeyInfoVos.get(storeyInfoVos.size() - 1);
        double floorNum = NumberUtil.div(Integer.parseInt(storey), 4);
        if (floorNum % 1 != 0) {
            floorNum = floorNum + 1;
        }
        int floorCount = (int) NumberUtil.mul((int) floorNum, 4);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < floorCount; i++) {
            list.add(Constants.ZERO);
        }

        for (String storeyInfoVo : storeyInfoVos) {
            list.set(floorCount - Integer.parseInt(storeyInfoVo), "1");
        }

        // 二进制数
        String binaryString = String.join("", list);
        // 转换为十进制数
        int binaryNumber = Integer.parseInt(binaryString, 2);
        // 转换为十六进制数
        String hexString = Integer.toHexString(binaryNumber);
        System.out.println("十六进制数: " + hexString);

        log.info("十六进制数hexString:" + hexString);
        String formattedNumber = String.format("%16s", hexString).replace(' ', '0');

        log.info("formattedNumber:" + formattedNumber);
        String zeroString = String.format("%052d", 0);

        log.info("zeroString:" + zeroString);
        String floor = "FF" + formattedNumber + zeroString;

        log.info("楼层floor：" + floor);
        return floor.toUpperCase();
    }
}
