package com.drew.metadata.avi;

import com.drew.imaging.riff.RiffHandler;
import com.drew.lang.ByteArrayReader;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Metadata;

import java.io.IOException;

/**
 * Implementation of {@link RiffHandler} specialising in Wav support.
 *
 * Extracts data from chunk/list types:
 *
 * <ul>
 *     <li><code>"avih"</code>: width, height, streams</li>
 *     <li><code>"strh"</code>: frames/second, samples/second, duration, video codec</li>
 * </ul>
 *
 * Sources: http://www.alexander-noe.com/video/documentation/avi.pdf
 *          https://msdn.microsoft.com/en-us/library/ms899422.aspx
 *          https://www.loc.gov/preservation/digital/formats/fdd/fdd000025.shtml
 *
 * @author Payton Garland
 */
public class AviRiffHandler implements RiffHandler
{
    @NotNull
    private final AviDirectory _directory;

    @NotNull
    private String _currentList = "";

    public AviRiffHandler(@NotNull Metadata metadata)
    {
        _directory = new AviDirectory();
        metadata.addDirectory(_directory);
    }

    public boolean shouldAcceptRiffIdentifier(@NotNull String identifier)
    {
        return identifier.equals(AviDirectory.FORMAT);
    }

    public boolean shouldAcceptChunk(@NotNull String fourCC)
    {
        return fourCC.equals(AviDirectory.CHUNK_STREAM_HEADER)
            || fourCC.equals(AviDirectory.CHUNK_MAIN_HEADER);
    }

    public boolean shouldAcceptList(@NotNull String fourCC)
    {
        if (fourCC.equals(AviDirectory.LIST_HEADER) || fourCC.equals(AviDirectory.LIST_STREAM_HEADER) || fourCC.equals(AviDirectory.FORMAT)) {
            _currentList = fourCC;
        } else {
            _currentList = "";
        }
        return fourCC.equals(AviDirectory.LIST_HEADER)
            || fourCC.equals(AviDirectory.LIST_STREAM_HEADER)
            || fourCC.equals(AviDirectory.FORMAT);
    }

    public void processChunk(@NotNull String fourCC, @NotNull byte[] payload)
    {
        try {
            if (fourCC.equals(AviDirectory.CHUNK_STREAM_HEADER)) {
                ByteArrayReader reader = new ByteArrayReader(payload);
                reader.setMotorolaByteOrder(false);

                String fccType = new String(reader.getBytes(0, 4));
                String fccHandler = new String(reader.getBytes(4, 4));
//                int dwFlags = reader.getInt32(8);
//                int wPriority = reader.getInt16(12);
//                int wLanguage = reader.getInt16(14);
//                int dwInitialFrames = reader.getInt32(16);
                float dwScale = reader.getFloat32(20);
                float dwRate = reader.getFloat32(24);
//                int dwStart = reader.getInt32(28);
                int dwLength = reader.getInt32(32);
//                int dwSuggestedBufferSize = reader.getInt32(36);
//                int dwQuality = reader.getInt32(40);
//                int dwSampleSize = reader.getInt32(44);
//                byte[] rcFrame = reader.getBytes(48, 2);

                if (fccType.equals("vids")) {
                    if (!_directory.containsTag(AviDirectory.TAG_FRAMES_PER_SECOND)) {
                        _directory.setDouble(AviDirectory.TAG_FRAMES_PER_SECOND, (dwRate / dwScale));

                        double duration = dwLength / (dwRate / dwScale);
                        Integer hours = (int) duration / (int) (Math.pow(60, 2));
                        Integer minutes = ((int) duration / (int) (Math.pow(60, 1))) - (hours * 60);
                        Integer seconds = (int) Math.round((duration / (Math.pow(60, 0))) - (minutes * 60));
                        String time = String.format("%1$02d:%2$02d:%3$02d", hours, minutes, seconds);

                        _directory.setString(AviDirectory.TAG_DURATION, time);
                        _directory.setString(AviDirectory.TAG_VIDEO_CODEC, fccHandler);
                    }
                } else if (fccType.equals("auds")) {
                    if (!_directory.containsTag(AviDirectory.TAG_SAMPLES_PER_SECOND)) {
                        _directory.setDouble(AviDirectory.TAG_SAMPLES_PER_SECOND, (dwRate / dwScale));
                    }
                }
            } else if (fourCC.equals(AviDirectory.CHUNK_MAIN_HEADER)) {
                ByteArrayReader reader = new ByteArrayReader(payload);
                reader.setMotorolaByteOrder(false);

//                int dwMicroSecPerFrame = reader.getInt32(0);
//                int dwMaxBytesPerSec = reader.getInt32(4);
//                int dwPaddingGranularity = reader.getInt32(8);
//                int dwFlags = reader.getInt32(12);
//                int dwTotalFrames = reader.getInt32(16);
//                int dwInitialFrames = reader.getInt32(20);
                int dwStreams = reader.getInt32(24);
//                int dwSuggestedBufferSize = reader.getInt32(28);
                int dwWidth = reader.getInt32(32);
                int dwHeight = reader.getInt32(36);
//                byte[] dwReserved = reader.getBytes(40, 4);

                _directory.setInt(AviDirectory.TAG_WIDTH, dwWidth);
                _directory.setInt(AviDirectory.TAG_HEIGHT, dwHeight);
                _directory.setInt(AviDirectory.TAG_STREAMS, dwStreams);
            }
        } catch (IOException ex) {
            _directory.addError(ex.getMessage());
        }
    }
}

@@ -862,16 +862,18 @@
 ctory;%0A%0A
+//
     @Not
@@ -869,32 +869,34 @@
 %0A//    @NotNull%0A
+//
     private Stri
@@ -1468,32 +1468,34 @@
 g fourCC)%0A    %7B%0A
+//
         if (four
@@ -1617,24 +1617,26 @@
 .FORMAT)) %7B%0A
+//
             
@@ -1658,16 +1658,18 @@
 fourCC;%0A
+//
         
@@ -1677,16 +1677,18 @@
  else %7B%0A
+//
         
@@ -1710,16 +1710,18 @@
 t = %22%22;%0A
+//
         

Comment unused code