/* GoalTemplateManager.js

   Manages goal-templates for leveling tab.

*/

(function() {
    "use strict";

    /*

      Data Format: 
      
      localStorage.goalTemplates
      array of templates

      template:
      { stype: <array of ship types> (e.g. ["DD","CL"..])
               (ship type should be normalized (all in uppercase))
               ("*" is a ship type wildcard)
      , map: [<world#>,<map#>]
      , rank: <E,D,C,B,A,S,SS> = <1,2,3,4,5,6,7>
      , flagship: true / false
      , mvp: true / false
      , enable: true / false
      }

     */

    window.GoalTemplateManager = {
        load: function() {
            return JSON.parse(localStorage.goalTemplates || "[]");
        },
        save: function(t) {
            try {
                if (!Array.isArray(t))
                    throw "not an array";
                localStorage.goalTemplates =
                    JSON.stringify(t);
            } catch (err) {
                console.log("error when saving:", err);
            }
        },
        newTemplate: function() {
            return { 
                stype: ["*"],
                map: [3,2],
                rank: 6,
                flagship: true,
                mvp: true,
                enable: true
            };
        },
        // parse ship type query
        parseSType: function(raw) {
            var parsed = raw
                .split(",")
                .map(function(x) { return x.trim(); } )
                .filter( function(x) { return x.length > 0; } )
                .map( function(x) { return x.toUpperCase(); } );
            // TODO: remove invalid & duplicate
            // TODO: make sure "*" is handled properly
            return parsed;
        },
        // return a string represetation of stype query
        showSType: function(stypes) {
            function translate(x) {
                return x === "*"? "Any":x;
            }
            return stypes.length === 0 ? "<Empty>" : stypes.map(translate).join(",");
        }
    };
})();

@@ -601,16 +601,42 @@
 ger = %7B%0A
+        validSTypes: %5B%5D,%0A%0A
         
@@ -1370,24 +1370,53 @@
 tion(raw) %7B%0A
+            var self = this;%0A
             
@@ -1693,16 +1693,297 @@
 plicate%0A
+            var shipTypes = %5B%5D;%0A%0A            if (parsed.indexOf(%22*%22) != -1)%0A                shipTypes.push(%22*%22);%0A%0A            $.each(self.validSTypes , function(i,stype) %7B%0A                if (parsed.indexOf(stype) != -1)%0A                    shipTypes.push(stype);%0A            %7D);%0A%0A
         
@@ -2048,22 +2048,25 @@
  return 
-parsed
+shipTypes
 ;%0A      
@@ -2356,14 +2356,463 @@
    %7D
-%0A    %7D
+,%0A        showInputSType: function(stypes) %7B%0A            return stypes.join(%22,%22);%0A        %7D%0A    %7D;%0A%0A    // initialize valid stypes%0A    var stypeRaw = %0A        %22DDE DD  CL  CLT %22 +%0A        %22CA  CAV CVL FBB %22 +%0A        %22BB  BBV CV  XBB %22 +%0A        %22SS  SSV AP  AV  %22 +%0A        %22LHA CVB AR  AS  %22 +%0A        %22CT  AO%22;%0A    window.GoalTemplateManager.validSTypes = %0A        stypeRaw%0A          .split(%22 %22)%0A          .filter( function(x) %7B return x.length %3E 0; %7D)
 ;%0A%7D)

normalize ship type