package org.atlasapi.output.writers;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;

import org.atlasapi.content.Broadcast;
import org.atlasapi.entity.Alias;
import org.atlasapi.output.EntityListWriter;
import org.atlasapi.output.FieldWriter;
import org.atlasapi.output.OutputContext;

import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import com.metabroadcast.common.ids.NumberToShortStringCodec;

public final class BroadcastWriter implements EntityListWriter<Broadcast> {
    
    private static final String ELEMENT_NAME = "broadcast";
    
    private final AliasWriter aliasWriter = new AliasWriter();
    private final BroadcastIdAliasMapping aliasMapping = new BroadcastIdAliasMapping();
    private final BlackoutRestrictionWriter blackoutRestrictionWriter = new BlackoutRestrictionWriter();

    private final String listName;
    private NumberToShortStringCodec codec;

    public BroadcastWriter(String listName, NumberToShortStringCodec codec) {
        this.listName = checkNotNull(listName);
        this.codec = checkNotNull(codec);
    }

    @Override
    public void write(Broadcast entity, FieldWriter writer, OutputContext ctxt) throws IOException {
        ImmutableList.Builder<Alias> aliases = ImmutableList.builder();
        Alias idAlias = aliasMapping.apply(entity);
        if (idAlias != null) {
            aliases.add(idAlias);
        }
        aliases.addAll(entity.getAliases());
        writer.writeList(aliasWriter, aliases.build(), ctxt);
        writer.writeField("transmission_time", entity.getTransmissionTime());
        writer.writeField("transmission_end_time", entity.getTransmissionEndTime());
        writer.writeField("broadcast_duration", Ints.saturatedCast(entity.getBroadcastDuration().getStandardSeconds()));
        writer.writeField("broadcast_on", codec.encode(entity.getChannelId().toBigInteger()));
        writer.writeField("schedule_date", entity.getScheduleDate());
        writer.writeField("repeat", entity.getRepeat());
        writer.writeField("subtitled", entity.getSubtitled());
        writer.writeField("signed",entity.getSigned());
        writer.writeField("audio_described",entity.getAudioDescribed());
        writer.writeField("high_definition",entity.getHighDefinition());
        writer.writeField("widescreen",entity.getWidescreen());
        writer.writeField("surround",entity.getSurround());
        writer.writeField("live",entity.getLive());
        writer.writeField("premiere",entity.getPremiere());
        writer.writeField("new_series",entity.getNewSeries());
        if(!entity.getBlackoutRestriction().isPresent()) {
            writer.writeField("blackout_restriction", null);
        } else {
            writer.writeObject(blackoutRestrictionWriter, entity.getBlackoutRestriction().get(), ctxt);
        }
    }

    @Override
    public String listName() {
        return listName;
    }

    @Override
    public String fieldName(Broadcast entity) {
        return ELEMENT_NAME;
    }
}
@@ -91,46 +91,244 @@
 heck
-NotNull;%0A%0Aimport java.io.IOException;%0A
+Argument;%0Aimport static com.google.common.base.Preconditions.checkNotNull;%0A%0Aimport java.io.IOException;%0A%0Aimport com.google.common.util.concurrent.Futures;%0Aimport org.atlasapi.channel.Channel;%0Aimport org.atlasapi.channel.ChannelResolver;
 %0Aimp
@@ -397,16 +397,58 @@
 .Alias;%0A
+import org.atlasapi.entity.util.Resolved;%0A
 import o
@@ -718,16 +718,126 @@
 ngCodec;
+%0Aimport org.atlasapi.query.common.QueryExecutionException;%0Aimport org.atlasapi.query.v4.channel.ChannelWriter;
 %0A%0Apublic
@@ -1231,16 +1231,106 @@
 riter();
+%0A    private final ChannelWriter channelWriter = new ChannelWriter(%22channels%22, %22channel%22);
 %0A%0A    pr
@@ -1370,16 +1370,22 @@
 private 
+final 
 NumberTo
@@ -1407,16 +1407,67 @@
 c codec;
+%0A    private final ChannelResolver channelResolver;
 %0A%0A    pu
@@ -1534,16 +1534,49 @@
 ec codec
+, ChannelResolver channelResolver
 ) %7B%0A    
@@ -1661,16 +1661,78 @@
 codec);%0A
+        this.channelResolver = checkNotNull(channelResolver);%0A
     %7D%0A%0A 
@@ -2526,24 +2526,342 @@
 nteger()));%0A
+        Channel channel = null;%0A        if (entity.getChannelId() != null) %7B%0A            channel = Futures.get(channelResolver.resolveIds(ImmutableList.of(entity.getChannelId())), IOException.class)%0A                    .getResources().first().get();%0A        %7D%0A        writer.writeObject(channelWriter, channel, ctxt);%0A
         writ

Write channel on broadcast