/*
 * Copyright 2022 Tomitribe and community
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tomitribe.crest.table;

import org.tomitribe.crest.api.PrintOutput;
import org.tomitribe.crest.api.table.Table;
import org.tomitribe.crest.api.interceptor.CrestContext;
import org.tomitribe.crest.api.interceptor.CrestInterceptor;
import org.tomitribe.crest.term.Screen;

import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Table
public class TableInterceptor {

    @CrestInterceptor
    public Object intercept(final CrestContext crestContext) {
        final Options options = Options.from(crestContext);

        final Object result = crestContext.proceed();

        if (result instanceof Iterable) {
            final Iterable<?> list = (Iterable<?>) result;
            return new TableOutput(list, options);
        }
        return result;
    }

    public static class TableOutput implements PrintOutput {
        private final Data data;
        private final Options options;

        public TableOutput(final Iterable<?> iterable, final Options options) {
            this.data = Formatting.asTable(iterable, options);
            this.options = options;
        }

        @Override
        public void write(final PrintStream out) throws IOException {

            final int guess = Screen.guessWidth();
            final int width = guess > 0 ? guess : 150;

            final Border.Builder builder = getBuilder();
            final org.tomitribe.crest.table.Table table = new org.tomitribe.crest.table.Table(data, builder.build(), width);

            table.format(out);
        }

        private Border.Builder getBuilder() {
            final String borderName = options.getBorder().name();

            final Method method;
            try {
                method = Border.class.getMethod(borderName);
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException("Border not found: " + borderName);
            }

            final Border.Builder builder;
            try {
                builder = (Border.Builder) method.invoke(null);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Unable to access method Border." + borderName);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException("Unable to call method Border." + borderName, e);
            }
            return builder;
        }
    }

}

@@ -685,52 +685,8 @@
 ut;%0A
-import org.tomitribe.crest.api.table.Table;%0A
 impo
@@ -738,16 +738,16 @@
 ontext;%0A
+
 import o
@@ -799,16 +799,60 @@
 ceptor;%0A
+import org.tomitribe.crest.api.table.Table;%0A
 import o
@@ -1024,16 +1024,107 @@
 .Method;
+%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;
 %0A%0A@Table
@@ -1502,32 +1502,32 @@
 list, options);%0A
-
         %7D%0A      
@@ -1512,32 +1512,262 @@
 ons);%0A        %7D%0A
+%0A        if (result instanceof Stream) %7B%0A            final Stream%3C?%3E stream = (Stream%3C?%3E) result;%0A            final List%3C?%3E list = stream.collect(Collectors.toList());%0A            return new TableOutput(list, options);%0A        %7D%0A%0A
         return r

Support Stream return values (tests needed)