package org.atlasapi.query.v4.schedule;

import java.io.IOException;

import org.atlasapi.output.EntityListWriter;
import org.atlasapi.output.OutputContext;
import org.atlasapi.output.QueryResultWriter;
import org.atlasapi.output.ResponseWriter;
import org.atlasapi.query.annotation.ActiveAnnotations;
import org.atlasapi.query.common.QueryContext;
import org.atlasapi.query.common.QueryResult;
import org.atlasapi.schedule.ChannelSchedule;

import com.google.common.collect.FluentIterable;

public class ScheduleQueryResultWriter implements QueryResultWriter<ChannelSchedule> {

    private final EntityListWriter<ChannelSchedule> scheduleWriter;

    public ScheduleQueryResultWriter(EntityListWriter<ChannelSchedule> scheduleWriter) {
        this.scheduleWriter = scheduleWriter;
    }

    @Override
    public void write(QueryResult<ChannelSchedule> result, ResponseWriter writer) throws IOException {
        writer.startResponse();
        writeResult(result, writer);
        writer.finishResponse();
    }

    private void writeResult(QueryResult<ChannelSchedule> result, ResponseWriter writer)
        throws IOException {

        OutputContext ctxt = outputContext(result.getContext());

        //TODO: train-wreck
        if (result.getContext().getAnnotations() == ActiveAnnotations.standard()) {
            writer.writeField("license",
                "In accessing this feed, you agree that you will only " +
                "access its contents for your own personal and non-commercial " +
                "use, and not for any commercial or other purposes, including " +
                "but not restricted to advertising or selling any goods or " +
                "services, including any third-party software applications " +
                "available to the general public.");
        }
        if (result.isListResult()) {
            FluentIterable<ChannelSchedule> resources = result.getResources();
            writer.writeList(scheduleWriter, resources, ctxt);
        } else {
            writer.writeObject(scheduleWriter, result.getOnlyResource(), ctxt);
        }
    }

    private OutputContext outputContext(QueryContext queryContext) {
        return new OutputContext(
            queryContext.getAnnotations(),
            queryContext.getApplicationSources()
        );
    }

}

@@ -243,64 +243,8 @@
 er;%0A
-import org.atlasapi.query.annotation.ActiveAnnotations;%0A
 impo
@@ -1143,620 +1143,8 @@
 );%0A%0A
-        //TODO: train-wreck%0A        if (result.getContext().getAnnotations() == ActiveAnnotations.standard()) %7B%0A            writer.writeField(%22license%22,%0A                %22In accessing this feed, you agree that you will only %22 +%0A                %22access its contents for your own personal and non-commercial %22 +%0A                %22use, and not for any commercial or other purposes, including %22 +%0A                %22but not restricted to advertising or selling any goods or %22 +%0A                %22services, including any third-party software applications %22 +%0A                %22available to the general public.%22);%0A        %7D%0A
     

remove license message