package components;

import feathers.controls.ToggleButton;
import feathers.skins.RectangleSkin;
import feathers.text.TextFormat;
import format.SVG;
import model.Constants;
import openfl.display.Shape;
import openfl.events.Event;
import openfl.geom.ColorTransform;
import openfl.text.Font;
import openfl.utils.Assets;
import t9.util.ColorTraces.*;

class ToolbarToggleButton extends ToggleButton {

    private var svgIconPath:String;
    private var svgIcon:Shape;
    private var unselectedColor:Int;
    private var selectedColor:Int;
    private var iconSize:Int;
    private var textSize:Int;


	public function new(pSvgIconPath:String = null, 
                        pUnselectedColor:Int = 0x00FF00, 
                        pSelectedColor:Int = 0x000000,
                        pIconSize:Int = 40,
                        pTextSize:Int = 16) {
        super();

        svgIconPath = pSvgIconPath;
        unselectedColor = pSelectedColor;
        selectedColor = pUnselectedColor;
        iconSize = pIconSize;
        textSize = pTextSize;

        //traceRed("iconSize : " + iconSize + " / textSize : " + textSize);
	}

    override private function initialize():Void {
        super.initialize();

        iconPosition = TOP;
        embedFonts = true;
        backgroundSkin = new RectangleSkin(SolidColor( 0xFF0000, 0));

        // icon
        if(svgIconPath != null){
            svgIcon = new Shape();
            colorizeIcon(unselectedColor);
            new SVG(svgIconPath).render(svgIcon.graphics);
            icon = svgIcon;
            icon.width = icon.height = iconSize;

            iconOffsetY = 0;
        }
        
        // text
        var fnt:Font = Assets.getFont(Constants.MONTSERRAT_REGULAR_400);
        textFormat = new TextFormat(fnt.fontName, textSize, unselectedColor);
        selectedTextFormat = new TextFormat(fnt.fontName, textSize, selectedColor);

        addEventListener(Event.CHANGE, onButtonStateChange);

    }

    private function colorizeIcon(pColor:Int) {
        
        var ct = new ColorTransform();
        ct.color = pColor;
        svgIcon.transform.colorTransform = ct;
    }

    private function onButtonStateChange(e:Event):Void {
        selected ? colorizeIcon(selectedColor) : colorizeIcon(unselectedColor);
    }
}
