//
//   Copyright 2016  Cityzen Data
//
//   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 io.warp10.script.processing.image;

import io.warp10.script.NamedWarpScriptFunction;
import io.warp10.script.WarpScriptStackFunction;
import io.warp10.script.WarpScriptException;
import io.warp10.script.WarpScriptStack;
import io.warp10.script.processing.ProcessingUtil;

import java.util.List;

import processing.core.PGraphics;
import processing.core.PImage;

/**
 * Call blend
 */
public class Pblend extends NamedWarpScriptFunction implements WarpScriptStackFunction {
  
  public Pblend(String name) {
    super(name);
  }
  
  @Override
  public Object apply(WarpScriptStack stack) throws WarpScriptException {
    
    List<Object> params = ProcessingUtil.parseParams(stack, 9, 10);
        
    PGraphics pg = (PGraphics) params.get(0);

    String mode = "";
    int imode = 0;
    if (10 == params.size()) {
      mode = params.get(9).toString();
    } else if (11 == params.size()) {
      mode = params.get(10).toString();
    }

    if ("BLEND".equals(mode)) {
      imode = PGraphics.BLEND;
    } else if ("ADD".equals(mode)) {
      imode = PGraphics.ADD;
    } else if ("SUBTRACT".equals(mode)) {
      imode = PGraphics.SUBTRACT;
    } else if ("DARKEST".equals(mode)) {
      imode = PGraphics.DARKEST;
    } else if ("LIGHTEST".equals(mode)) {
      imode = PGraphics.LIGHTEST;
    } else if ("DIFFERENCE".equals(mode)) {
      imode = PGraphics.DIFFERENCE;
    } else if ("EXCLUSION".equals(mode)) {
      imode = PGraphics.EXCLUSION;
    } else if ("MULTIPLY".equals(mode)) {
      imode = PGraphics.MULTIPLY;
    } else if ("SCREEN".equals(mode)) {
      imode = PGraphics.SCREEN;
    } else if ("OVERLAY".equals(mode)) {
      imode = PGraphics.OVERLAY;
    } else if ("HARD_LIGHT".equals(mode)) {
      imode = PGraphics.HARD_LIGHT;
    } else if ("SOFT_LIGHT".equals(mode)) {
      imode = PGraphics.SOFT_LIGHT;
    } else if ("DODGE".equals(mode)) {
      imode = PGraphics.DODGE;
    } else if ("BURN".equals(mode)) {
      imode = PGraphics.BURN;
    } else {
      throw new WarpScriptException(getName() + ": invalid mode, should be 'BLEND', 'ADD', 'SUBTRACT', 'LIGHTEST', 'DARKEST', 'DIFFERENCE', 'EXCLUSION', 'MULTIPLY', 'SCREEN', 'OVERLAY', 'HARD_LIGHT', 'SOFT_LIGHT', 'DODGE', 'BURN'.");
    }


    if (10 == params.size()) {
      pg.blend(
        ((Number) params.get(1)).intValue(),
        ((Number) params.get(2)).intValue(),
        ((Number) params.get(3)).intValue(),
        ((Number) params.get(4)).intValue(),
        ((Number) params.get(5)).intValue(),
        ((Number) params.get(6)).intValue(),
        ((Number) params.get(7)).intValue(),
        ((Number) params.get(8)).intValue(),
        imode
      );
    } else if (11 == params.size()) {
      pg.blend(        
        (PImage) params.get(1),
        ((Number) params.get(2)).intValue(),
        ((Number) params.get(3)).intValue(),
        ((Number) params.get(4)).intValue(),
        ((Number) params.get(5)).intValue(),
        ((Number) params.get(6)).intValue(),
        ((Number) params.get(7)).intValue(),
        ((Number) params.get(8)).intValue(),
        ((Number) params.get(9)).intValue(),
        imode
      );          
    }
    
    stack.push(pg);
        
    return stack;
  }
}

@@ -1365,1492 +1365,12 @@
 0);%0A
-%0A
     
-String mode = %22%22;%0A    int imode = 0;%0A    if (10 == params.size()) %7B%0A      mode = params.get(9).toString();%0A    %7D else if (11 == params.size()) %7B%0A      mode = params.get(10).toString();%0A    %7D%0A%0A    if (%22BLEND%22.equals(mode)) %7B%0A      imode = PGraphics.BLEND;%0A    %7D else if (%22ADD%22.equals(mode)) %7B%0A      imode = PGraphics.ADD;%0A    %7D else if (%22SUBTRACT%22.equals(mode)) %7B%0A      imode = PGraphics.SUBTRACT;%0A    %7D else if (%22DARKEST%22.equals(mode)) %7B%0A      imode = PGraphics.DARKEST;%0A    %7D else if (%22LIGHTEST%22.equals(mode)) %7B%0A      imode = PGraphics.LIGHTEST;%0A    %7D else if (%22DIFFERENCE%22.equals(mode)) %7B%0A      imode = PGraphics.DIFFERENCE;%0A    %7D else if (%22EXCLUSION%22.equals(mode)) %7B%0A      imode = PGraphics.EXCLUSION;%0A    %7D else if (%22MULTIPLY%22.equals(mode)) %7B%0A      imode = PGraphics.MULTIPLY;%0A    %7D else if (%22SCREEN%22.equals(mode)) %7B%0A      imode = PGraphics.SCREEN;%0A    %7D else if (%22OVERLAY%22.equals(mode)) %7B%0A      imode = PGraphics.OVERLAY;%0A    %7D else if (%22HARD_LIGHT%22.equals(mode)) %7B%0A      imode = PGraphics.HARD_LIGHT;%0A    %7D else if (%22SOFT_LIGHT%22.equals(mode)) %7B%0A      imode = PGraphics.SOFT_LIGHT;%0A    %7D else if (%22DODGE%22.equals(mode)) %7B%0A      imode = PGraphics.DODGE;%0A    %7D else if (%22BURN%22.equals(mode)) %7B%0A      imode = PGraphics.BURN;%0A    %7D else %7B%0A      throw new WarpScriptException(getName() + %22: invalid mode, should be 'BLEND', 'ADD', 'SUBTRACT', 'LIGHTEST', 'DARKEST', 'DIFFERENCE', 'EXCLUSION', 'MULTIPLY', 'SCREEN', 'OVERLAY', 'HARD_LIGHT', 'SOFT_LIGHT', 'DODGE', 'BURN'.%22);%0A    %7D%0A%0A
 %0A   
@@ -1773,37 +1773,67 @@
 alue(),%0A        
-imode
+((Number) params.get(9)).intValue()
 %0A      );%0A    %7D 
@@ -2292,13 +2292,44 @@
     
-imode
+((Number) params.get(10)).intValue()
 %0A   

Revert "Correction of Pblend signature"