package com.drew.imaging.heif;

import com.drew.lang.StreamReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.heif.boxes.Box;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DataFormatException;

public class HeifReader
{
    private StreamReader reader;
    private int tabCount;


    public void extract(Metadata metadata, InputStream inputStream, HeifHandler handler) throws IOException, DataFormatException
    {
        reader = new StreamReader(inputStream);
        reader.setMotorolaByteOrder(true);

        boolean printVisited = false;
        tabCount = 0;

        if (printVisited) {
            System.out.println("_______________Beginning to Print Tree_______________");
            System.out.println("| \"\" = leaf      \"[]\" = container    \"{}\" = Unknown |");
            System.out.println("_____________________________________________________");
        }
        processBoxes(reader, -1, handler, printVisited);
    }

    private void processBoxes(StreamReader reader, long atomEnd, HeifHandler heifHandler, boolean printVisited)
    {
        try {
            while ((atomEnd == -1) ? true : reader.getPosition() < atomEnd) {

                Box box = new Box(reader);

                /*
                 * Determine if fourCC is container/atom and process accordingly
                 * Unknown atoms will be skipped
                 */
                if (heifHandler.shouldAcceptContainer(box)) {

                    if (printVisited) {
                        for (int i = 0; i < tabCount; i++) {
                            System.out.print("   " + i + "   |");
                        }
                        System.out.println(" [" + box.type + "]");
                        tabCount++;
                    }

                    heifHandler.processContainer(box, reader);
                    processBoxes(reader, box.size + reader.getPosition() - 8, heifHandler, printVisited);

                    if (printVisited) {
                        tabCount--;
                    }


                } else if (heifHandler.shouldAcceptBox(box)) {

                    if (printVisited) {
                        for (int i = 0; i < tabCount; i++) {
                            System.out.print("   " + i + "   |");
                        }
                        System.out.println("  " + box.type);
                    }

                    heifHandler = heifHandler.processBox(box, reader.getBytes((int)box.size - 8));

                } else {

                    if (box.size > 1) {
                        reader.skip(box.size - 8);
                    } else if (box.size == -1) {
                        break;
                    }

                    if (printVisited) {
                        for (int i = 0; i < tabCount; i++) {
                            System.out.print("   " + i + "   |");
                        }
                        System.out.println(" {" + box.type + "}");
                    }

                }
            }
        } catch (IOException ignored) {
            // Currently, reader relies on IOException to end
        }
    }
}

@@ -554,380 +554,8 @@
 );%0A%0A
-        boolean printVisited = false;%0A        tabCount = 0;%0A%0A        if (printVisited) %7B%0A            System.out.println(%22_______________Beginning to Print Tree_______________%22);%0A            System.out.println(%22%7C %5C%22%5C%22 = leaf      %5C%22%5B%5D%5C%22 = container    %5C%22%7B%7D%5C%22 = Unknown %7C%22);%0A            System.out.println(%22_____________________________________________________%22);%0A        %7D%0A
     
@@ -590,30 +590,16 @@
  handler
-, printVisited
 );%0A    %7D
@@ -692,30 +692,8 @@
 dler
-, boolean printVisited
 )%0A  
@@ -1068,938 +1068,225 @@
 ) %7B%0A
-%0A                    if (printVisited) %7B%0A                        for (int i = 0; i %3C tabCount; i++) %7B%0A                            System.out.print(%22   %22 + i + %22   %7C%22);%0A                        %7D%0A                        System.out.println(%22 %5B%22 + box.type + %22%5D%22);%0A                        tabCount++;%0A                    %7D%0A%0A                    heifHandler.processContainer(box, reader);%0A                    processBoxes(reader, box.size + reader.getPosition() - 8, heifHandler, printVisited);%0A%0A                    if (printVisited) %7B%0A                        tabCount--;%0A                    %7D%0A%0A%0A                %7D else if (heifHandler.shouldAcceptBox(box)) %7B%0A%0A                    if (printVisited) %7B%0A                        for (int i = 0; i %3C tabCount; i++) %7B%0A                            System.out.print(%22   %22 + i + %22   %7C%22);%0A                        %7D%0A                        System.out.println(%22  %22 + box.type);%0A                    %7D%0A
+                    heifHandler.processContainer(box, reader);%0A                    processBoxes(reader, box.size + reader.getPosition() - 8, heifHandler);%0A                %7D else if (heifHandler.shouldAcceptBox(box)) %7B
 %0A   
@@ -1381,17 +1381,16 @@
  - 8));%0A
-%0A
         
@@ -1406,17 +1406,16 @@
  else %7B%0A
-%0A
         
@@ -1603,292 +1603,8 @@
   %7D%0A
-%0A                    if (printVisited) %7B%0A                        for (int i = 0; i %3C tabCount; i++) %7B%0A                            System.out.print(%22   %22 + i + %22   %7C%22);%0A                        %7D%0A                        System.out.println(%22 %7B%22 + box.type + %22%7D%22);%0A                    %7D%0A%0A
     

Remove tree printing option from reader