/*
 * Copyright 2018 The Apache Software Foundation.
 *
 * 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
 *
 *      http://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.apache.pdfbox.pdmodel.interactive.annotation;

import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDAppearanceHandler;
import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDInkAppearanceHandler;

/**
 *
 * @author Paul King
 */
public class PDAnnotationInk extends PDAnnotationMarkup
{
    /**
     * The type of annotation.
     */
    public static final String SUB_TYPE = "Ink";

    private PDAppearanceHandler inkAppearanceHandler;

    /**
     * Constructor.
     */
    public PDAnnotationInk()
    {
        getCOSObject().setName(COSName.SUBTYPE, SUB_TYPE);
    }

    /**
     * Constructor.
     *
     * @param dict The annotations dictionary.
     */
    public PDAnnotationInk(COSDictionary dict)
    {
        super(dict);
    }


    //TODO setInkList, javadoc
 
    public float[][] getInkList()
    {
        COSBase base = getCOSObject().getDictionaryObject(COSName.INKLIST);
        if (base instanceof COSArray)
        {
            COSArray array = (COSArray) base;
            float[][] inkList = new float[array.size()][];
            for (int i = 0; i < array.size(); ++i)
            {
                //TODO check for class
                COSArray innerArray = (COSArray) array.getObject(i);
                inkList[i] = innerArray.toFloatArray();
            }
            return inkList;
        }
        // Should never happen as this is a required item
        return null; 
    }

    /**
     * Set a custom appearance handler for generating the annotations appearance streams.
     * 
     * @param inkAppearanceHandler
     */
    public void setCustomInkAppearanceHandler(PDAppearanceHandler inkAppearanceHandler)
    {
        this.inkAppearanceHandler = inkAppearanceHandler;
    }

    @Override
    public void constructAppearances()
    {
        if (inkAppearanceHandler == null)
        {
            PDInkAppearanceHandler appearanceHandler = new PDInkAppearanceHandler(this);
            appearanceHandler.generateAppearanceStreams();
        }
        else
        {
            inkAppearanceHandler.generateAppearanceStreams();
        }
    }
}

@@ -1558,41 +1558,1014 @@
  %7D%0A%0A
-%0A    //TODO setInkList, javadoc%0A 
+    /**%0A     * Sets the paths that make this annotation.%0A     *%0A     * @param inkList An array of arrays, each representing a stroked path. Each array shall be a%0A     * series of alternating horizontal and vertical coordinates. If the parameter is null the entry%0A     * will be removed.%0A     */%0A    public void setInkList(float%5B%5D%5B%5D inkList)%0A    %7B%0A        if (inkList == null)%0A        %7B%0A            getCOSObject().removeItem(COSName.INKLIST);%0A            return;%0A        %7D%0A        COSArray array = new COSArray();%0A        for (float%5B%5D path : inkList)%0A        %7B%0A            COSArray innerArray = new COSArray();%0A            innerArray.setFloatArray(path);%0A            array.add(innerArray);%0A        %7D%0A        getCOSObject().setItem(COSName.INKLIST, array);%0A    %7D%0A%0A    /**%0A     * Get one or more disjoint paths that make this annotation.%0A     *%0A     * @return An array of arrays, each representing a stroked path. Each array shall be a series of%0A     * alternating horizontal and vertical coordinates.%0A     */
 %0A   
@@ -2915,139 +2915,173 @@
     
-//TODO check for class%0A                COSArray innerArray = (COSArray) array.getObject(i);%0A                inkList%5Bi%5D = innerArray
+COSBase base2 = array.getObject(i);%0A                if (base2 instanceof COSArray)%0A                %7B%0A                    inkList%5Bi%5D = ((COSArray) array.getObject(i))
 .toF
@@ -3097,32 +3097,36 @@
 ();%0A            
+    
 %7D%0A            re
@@ -3127,32 +3127,96 @@
     
-return inkList;%0A
+    else%0A                %7B%0A                    inkList%5Bi%5D = new float%5B0%5D;%0A      
         
 %7D%0A  
@@ -3203,32 +3203,34 @@
 ;%0A              
+  
 %7D%0A        // Sho
@@ -3227,57 +3227,51 @@
     
-// Should never happen as this is a required item
+    %7D%0A            return inkList;%0A        %7D
 %0A   
@@ -3287,13 +3287,23 @@
 rn n
-ull; 
+ew float%5B0%5D%5B0%5D;
 %0A   

add method to set inkList, javadoc, avoid ClassCastException