package components;

import model.AppModelLocator;
import openfl.display.Bitmap;
import openfl.system.ApplicationDomain;
import openfl.system.LoaderContext;
import openfl.display.LoaderInfo;
import openfl.net.URLRequest;
import openfl.display.Loader;
import openfl.events.Event;
import feathers.controls.AssetLoader;
import feathers.layout.AnchorLayoutData;
import feathers.layout.AnchorLayout;
import feathers.text.TextFormat;
import openfl.Assets;
import openfl.text.Font;
import feathers.controls.Label;
import feathers.layout.HorizontalLayout;
import openfl.display.Shape;
import model.Constants;
import feathers.skins.RectangleSkin;
import feathers.controls.LayoutGroup;
import t9.util.ColorTraces.*;

class RoundAvatar extends LayoutGroup {

	private var avatarSize:Int;
	private var riderUID:String;

	public function new(pAvatarSize:Int = 32, pRiderUID:String) {
		super();
		avatarSize = pAvatarSize;
		riderUID = pRiderUID;
	}

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

		/*var hl:HorizontalLayout = new HorizontalLayout();
		hl.horizontalAlign = LEFT;
		layout = hl;*/

		var bkgs = new RectangleSkin(SolidColor(0xFFFFFF, 0));
		bkgs.width = Std.int(avatarSize * 1.2);
		bkgs.height = avatarSize;
		backgroundSkin = bkgs;

		/*var al:AssetLoader = new AssetLoader();
        al.addEventListener(Event.COMPLETE, onLoadComplete);
		al.sourceScale = 0.6;
		al.source = "https://ui-avatars.com/api/?background=random&size=128";
        addChild(al);*/

		var m:Shape = new Shape();
		m.graphics.beginFill(Std.int(Math.random() * 0xFFFFFF), 1);
		m.graphics.drawCircle(avatarSize / 2, avatarSize / 2, avatarSize / 2);
		m.graphics.endFill();
		addChild(m);
		mask = m;

        var l:Loader = new Loader();
		//l.load(new URLRequest("https://lepetittrot.com/lptcm/assets/profile-pictures/pp256/pp_test_256.jpg"));
		l.load(new URLRequest(Constants.PROFIL_PICTURES_PATH_512 + riderUID + ".jpg"));
        l.contentLoaderInfo.addEventListener (Event.COMPLETE, onLoadComplete);
		addChild(l);
		
	}

    private function onLoadComplete(e:Event) {

        var l:Loader = cast(e.currentTarget, LoaderInfo).loader;
		var bmp:Bitmap = e.currentTarget.content;
   		bmp.smoothing = true;
		l.content.width = l.content.height = avatarSize;

    }
}
