package com.muyu.rule.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.muyu.etl.domain.DataValue;
import com.muyu.etl.enums.DataType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.util.Arrays;
import java.util.Iterator;

/**
 * @Author：张承志
 * @Package：com.muyu.rule.common.utils
 * @Project：cloud-etl-rule
 * @name：  数组压栈工具
 * @Date：2024/9/10 14:46
 */
@Data
@Builder
@AllArgsConstructor
@Log4j2
public class ArrayUtil<T>  implements Iterator<T> {


    private final T[] originalArray; // 原始数组
    private T[] currentArray; // 当前数组，用于迭代
    private int currentIndex; // 当前索引
    private int lastReturnedIndex; // 上一次调用next()方法时返回的索引
    private boolean canRemove = false; // 标记是否可以移除元素

    public ArrayUtil(T[] array) {
        this.originalArray = array;
        this.currentArray = Arrays.copyOf(array, array.length);
        this.currentIndex = 0;
    }

    @Override
    public boolean hasNext() {
        return currentIndex < currentArray.length;
    }

    @Override
    public T next() {
        if (!hasNext()) {
            throw new IllegalStateException("No more elements");
        }
        lastReturnedIndex = currentIndex;
        canRemove = true; // 调用next()后可以调用remove()
        return currentArray[currentIndex++];
    }

    @Override
    public void remove() {
        if (!canRemove) {
            throw new IllegalStateException("Cannot call remove() without calling next()");
        }
        // 删除元素并更新数组
        System.arraycopy(currentArray, lastReturnedIndex + 1, currentArray, lastReturnedIndex, currentArray.length - lastReturnedIndex - 1);
        currentArray = Arrays.copyOf(currentArray, currentArray.length - 1); // 缩小数组大小
        currentIndex--; // 因为我们移除了当前元素，所以索引应该减一
        canRemove = false;
    }

    public static void main(String[] args) {
        DataValue[] dataStructures = new DataValue[6];
        dataStructures[0] = DataValue.builder().value("aaa1").key("name").type(DataType.VARCHAR).label("姓名").build();
        dataStructures[1] = DataValue.builder().value("aaa2").key("name").type(DataType.VARCHAR).label("姓名").build();
        dataStructures[2] = DataValue.builder().value("aaa3").key("name").type(DataType.VARCHAR).label("姓名").build();
        dataStructures[3] = DataValue.builder().value("aaa4").key("name").type(DataType.VARCHAR).label("姓名").build();
        dataStructures[4] = DataValue.builder().value("aaa5").key("name").type(DataType.VARCHAR).label("姓名").build();
        dataStructures[5] = DataValue.builder().value("aaa6").key("name").type(DataType.VARCHAR).label("姓名").build();

        ArrayUtil<DataValue> dataStructureArrayUtil = new ArrayUtil<>(dataStructures);
        log.info(dataStructureArrayUtil.originalArray.length);
        log.info(JSONObject.toJSONString(dataStructureArrayUtil.originalArray));
        while (dataStructureArrayUtil.hasNext()){
            DataValue next = dataStructureArrayUtil.next();
            if (next.getValue().equals("aaa3")){
                dataStructureArrayUtil.remove();
            }
        }
        log.info(dataStructureArrayUtil.currentArray.length);
        log.info(Arrays.toString(dataStructureArrayUtil.currentArray));

    }


}
