package com.quinn.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.quinn.common.EditText;
import com.quinn.common.WangEdit;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class ContentUtil {

    public static String toTextContentFromWangEdit(String wangEdit){
        List<WangEdit> decode = JsonUtils.decode(wangEdit, new TypeReference<List<WangEdit>>(){});
        StringBuffer sb = new StringBuffer();
        decode.forEach(x->{
            List<EditText> children = x.getChildren();
            if (!CollectionUtils.isEmpty(children)){
                children.forEach(y ->{
                    getChildText(sb,y);
                });
            }
        });
        return sb.toString();
    }

    private static void getChildText(StringBuffer sb, EditText editText){
        List<EditText> children = editText.getChildren();
        if (!CollectionUtils.isEmpty(children)){
            children.forEach(y ->{
                getChildText(sb,y);
            });
        }else {
            sb.append(editText.getText());
        }
    }

}
