/*
 * Copyright 2002-2019 Drew Noakes and contributors
 *
 *    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.
 *
 * More information about this project is available at:
 *
 *    https://drewnoakes.com/code/exif/
 *    https://github.com/drewnoakes/metadata-extractor
 */
package com.drew.metadata.mp4.boxes;

import com.drew.lang.SequentialReader;
import com.drew.metadata.mp4.Mp4Directory;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.drew.metadata.mp4.Mp4Directory.TAG_LATITUDE;
import static com.drew.metadata.mp4.Mp4Directory.TAG_LONGITUDE;

public class UserDataBox extends FullBox {

    private static final String LOCATION_CODE = "©xyz";

    private static final Pattern COORDINATE_PATTERN = Pattern.compile("([+-]\\d+\\.\\d+)([+-]\\d+\\.\\d+)");

    private String coordinateString;

    public UserDataBox(final SequentialReader reader, final Box box) throws IOException {
        super(reader, box);

        final String fourCC = getFourCc(reader);

        if (LOCATION_CODE.equals(fourCC)) {
            extractLocation(reader);
        }
    }

    private String getFourCc(final SequentialReader reader) throws IOException {
        final byte[] codeBytes = reader.getBytes(4);
        return new String(codeBytes, "ISO-8859-1");
    }

    private void extractLocation(final SequentialReader reader) throws IOException {
        final int length = reader.getUInt16();
        reader.skip(2);
        final byte[] bytes = reader.getBytes(length);
        coordinateString = new String(bytes, "UTF-8");
    }

    public void addMetadata(final Mp4Directory directory) {
        final Matcher matcher = COORDINATE_PATTERN.matcher(coordinateString);
        if (matcher.find()) {
            final double latitude = Double.parseDouble(matcher.group(1));
            final double longitude = Double.parseDouble(matcher.group(2));

            directory.setDouble(TAG_LATITUDE, latitude);
            directory.setDouble(TAG_LONGITUDE, longitude);
        }
    }
}

@@ -867,24 +867,67 @@
 tialReader;%0A
+import com.drew.lang.annotations.Nullable;%0A
 import com.d
@@ -1386,24 +1386,38 @@
 %5C.%5C%5Cd+)%22);%0A%0A
+    @Nullable%0A
     private 
@@ -2230,24 +2230,68 @@
 irectory) %7B%0A
+        if (coordinateString != null) %7B%0A    
         fina
@@ -2360,24 +2360,28 @@
 g);%0A        
+    
 if (matcher.
@@ -2394,32 +2394,36 @@
 ) %7B%0A            
+ 
+   
 final double lat
@@ -2468,16 +2468,20 @@
 up(1));%0A
+    
         
@@ -2552,32 +2552,36 @@
 );%0A%0A            
+   
+ 
 directory.setDou
@@ -2601,32 +2601,36 @@
 UDE, latitude);%0A
+    
             dire
@@ -2664,32 +2664,46 @@
 DE, longitude);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix NPE