/*
 * Aeronica's mxTune MOD
 * Copyright 2019, Paul Boese a.k.a. Aeronica
 *
 *   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 net.aeronica.libs.mml.readers.ms2mml;

import net.aeronica.libs.mml.core.MMLAllowedCharacters;
import net.aeronica.libs.mml.readers.AbstractMmlFileReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.nio.file.Path;

public class Ms2MmlReader extends AbstractMmlFileReader
{

    public Ms2MmlReader() { /* NOP*/ }

    @Override
    public String getFileExtension()
    {
        return "ms2mml";
    }

    @Override
    public boolean parseFile(Path path)
    {
        readMs2Mml(getFile(path));
        return hasErrors();
    }

    @Override
    public String getMML()
    {
        return mml;
    }

    private void readMs2Mml(InputStream is)
    {
        Ms2 ms2;
        StringBuilder builder =  new StringBuilder("MML@");
        try
        {
            JAXBContext jaxbContext = JAXBContext.newInstance("net.aeronica.libs.mml.readers.ms2mml");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            ms2 = (Ms2) unmarshaller.unmarshal(is);

        if (ms2 != null)
        {
            String melodyValue = MMLAllowedCharacters.filterAllowedCharacters(ms2.melody);
            if (!melodyValue.equals(""))
                builder.append(melodyValue);

            for (Ms2.Chord chord : ms2.chord)
            {
                String chordValue = MMLAllowedCharacters.filterAllowedCharacters(chord.value);
                if ((chordValue.equals("")))
                    continue;
                builder.append(",");
                builder.append(chordValue);
            }
            builder.append(";");
            mml = builder.toString();
        }
        }
        catch (JAXBException e)
        {
            addError(e.getLocalizedMessage());
        }
    }
}

@@ -1438,68 +1438,8 @@
 s2;%0A
-        StringBuilder builder =  new StringBuilder(%22MML@%22);%0A
     
@@ -1690,24 +1690,28 @@
 );%0A%0A        
+    
 if (ms2 != n
@@ -1715,34 +1715,109 @@
 = null)%0A        
-%7B%0A
+    %7B%0A                StringBuilder builder = new StringBuilder(%22MML@%22);%0A    
             Stri
@@ -1899,24 +1899,28 @@
             
+    
 if (!melodyV
@@ -1936,16 +1936,20 @@
 ls(%22%22))%0A
+    
         
@@ -1998,16 +1998,20 @@
         
+  
+  
 for (Ms2
@@ -2040,34 +2040,42 @@
 rd)%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -2165,24 +2165,28 @@
             
+    
 if ((chordVa
@@ -2222,16 +2222,20 @@
         
+    
 continue
@@ -2244,32 +2244,36 @@
                 
+  
+  
 builder.append(%22
@@ -2273,24 +2273,28 @@
 ppend(%22,%22);%0A
+    
             
@@ -2329,34 +2329,42 @@
 e);%0A            
+   
+ 
 %7D%0A
+    
             buil
@@ -2376,24 +2376,28 @@
 ppend(%22;%22);%0A
+    
             
@@ -2422,30 +2422,24 @@
 ring();%0A    
-    %7D%0A
         %7D%0A  
@@ -2443,16 +2443,18 @@
 %0A       
+ %7D
  catch (
@@ -2523,24 +2523,46 @@
 Message());%0A
+            mml = %22%22;%0A
         %7D%0A  

Make sure half-ass'd MML is not made available in the case of an error.