import {Banner3d,Banner2d, GuiFactory,Gui3dFactory, Mesh3d,LayerMask} from './box3.js';

/**创建设备3D图片icon
 * 
 * @param {Mesh3d} o3d 设备模型对象
 * @param {string} url 贴图路径
 * @param {{key:string,width:number,height:number,mode:number,angleY:number}} options {width:宽度, height:高度, mode:模式(0-不动，1-图标永远望向摄像机， 2-Y轴指向摄像机), angleY:Y轴旋转角度}
 * @returns 
 */
export function create3dImageToModel(o3d, url, options = { key: null, width: 1, height: 1, mode: 0, angleY: 0}) {
    options.key ? options.key = options.key : null;
    options.width ? options.width = options.width : options.width = 1;
    options.height ? options.height = options.height : options.height = 1;
    options.mode ? options.mode = options.mode : options.mode = 0;
    options.angleY ? options.angleY = options.angleY : options.angleY = 0;

    let img = GuiFactory.CreateImageButton(url);
    let b3d = new Banner3d(o3d, {
        key: options.key,
        width: options.width,
        height: options.height
    });
    b3d.addGui(img);
    
    b3d.mode = options.mode;
    b3d.transform.angleY = options.angleY;
    b3d.banner.isPickable = false;
    return b3d;
}

/**创建设备3D图片icon
 * 
 * @param {Mesh3d} o3d 设备模型对象
 * @param {string} url 贴图路径
 * @param {string} text 文字
 * @param {{key:string,width:number,height:number,mode:number,angleY:number}} options {width:宽度, height:高度, mode:模式(0-不动，1-图标永远望向摄像机， 2-Y轴指向摄像机), angleY:Y轴旋转角度}
 * @returns 
 */
 export function create3dButtonToModel(o3d, url,text, options = { key: null, width: 1, height: 1, mode: 0, angleY: 0}) {
    options.key ? options.key = options.key : null;
    options.width ? options.width = options.width : options.width = 1;
    options.height ? options.height = options.height : options.height = 1;
    options.mode ? options.mode = options.mode : options.mode = 0;
    options.angleY ? options.angleY = options.angleY : options.angleY = 0;

    let img = GuiFactory.CreateButton('3d-button',text,url);
    let b3d = new Banner3d(o3d, {
        key: options.key,
        width: options.width,
        height: options.height
    });
    b3d.addGui(img);
    
    b3d.mode = options.mode;
    b3d.transform.angleY = options.angleY;
    b3d.banner.isPickable = false;
    return b3d;
}

/**创建2d图标
 * 
 * @param {Mesh3d} o3d 设备模型对象
 * @param {string} url 贴图路径
 * @param {*} options 
 * @returns 
 */
export function create2dImageToModel (o3d,url, options={key:Math.random().toString(), width:50, height:50}){
    let imgbtn = GuiFactory.CreateImageButton(url);
    let b2d = new Banner2d(o3d, imgbtn,{key:options.key});
    options.width ? b2d.style.widthInPixels = options.width : b2d.style.widthInPixels = 40;
    options.height ? b2d.style.heightInPixels = options.height : b2d.style.heightInPixels = 40;
    b2d.abstractGui.guiAction.addMouseLeftClickAction(()=>{})
    return b2d;
}

 /**创建2D按钮 */
export function create2dButton(options={name,img,width,height,offsetX,offsetY,}){
   let btn = GuiFactory.CreateButton('', options.name,options.img);
   let b2d = new Banner2d(this.followPoint, btn, {key:options.name+'按钮'});
   b2d.style.widthInPixels = options.width;
   b2d.style.heightInPixels = options.height;
   b2d.style.border = 0;
   //b2d.style.backgroundColor = '#0f00ff';
   b2d.style.textColor = "#ffffff";
   b2d.offsetX = options.offsetX;
   b2d.offsetY = options.offsetY;

   b2d.style.fontWeight = 700;
   return b2d;
}

export function create3dTextToFloor(s3d, text, options = {
    key: null,
    color: '#fff',
    height: 1,
    angleY: 0,
    side: null
}) {
    options.key ? options.key = options.key : null;
    options.color ? options.color = options.color : options.color = '#fff';
    options.height ? options.height = options.height : options.height = 2;
    options.angleY ? options.angleY = options.angleY : options.angleY = 180;
    options.side ? options.side = options.side : options.side = 'left';
    if ((s3d.modelName.indexOf('#floor') === -1) || !s3d.uid) return;
    let txt = GuiFactory.CreateText(text);
    let b3d = new Banner3d(s3d, {
        key: options.key,
        width: 0,
        height: options.height
    });
    b3d.addGui(txt)
    b3d.transform.angleY = options.angleY
    b3d.style.color = options.color;

    let w1 = s3d.transform.width;
    let w2 = b3d.transform.width;
    let offset = (w1 + w2) / 2;

    if (options.side === 'right')
        b3d.transform.moveX(offset);
    else
        b3d.transform.moveX(-offset);
    //新版本Banner3d 初始化时的默认位置在对象上方，所以这里需要向下移动
    b3d.transform.moveY(-s3d.transform.height);
    b3d.isPickable = false;
    return b3d;
}

/**创建3d按钮
 * 
 * @param {string} text         文字
 * @param {string} url          图片路径
 * @param {string} width        宽度
 * @param {number} height       高度
 * @param {number} orientation  方向
 * @returns 
 */
export function create3dButton(text,url, width, height, orientation = 1){
    let btn3d = Gui3dFactory.CreateButton3d(text, url,{
        width:width,
        height:height,
        orientation:orientation
    })
    if(btn3d){
        //btn3d.mode = 2; //默认不使用朝向摄像机， 空间如果是子级，父级又旋转了，此状态下 朝向计算会出现矩阵错误，从自身选择变成绕轴旋转---未知bug 
        btn3d.mode = 0;
        btn3d.useMeshPicking = true;
        btn3d.isPickable = true;
        let style = btn3d.gui.style;
        //style.alpha = 0.9;
        //无背景
        style.backgroundColor = null;
        //字体白色
        style.textColor = "#ffffffff";
        //缩小字体
        style.fontSize /= 2;
        //加粗
        style.fontWeight = 700;
    }
    btn3d.layerMask = LayerMask.DefaultAndUI
    return btn3d;
}

export function create2dTextToModel(o3d,text,size, options={key:Math.random().toString(), width:50, height:50}){
    let txt = GuiFactory.CreateText(text);
    txt.style.fontSize = size;
    // txt.text.resizeToFit = true;
    // txt.text.textWrapping = true;
    txt.style.textOutlineWidth  = 1;
    txt.style.textOutlineColor = "blue"
    let b2d = new Banner2d(o3d, txt,{key:options.key})
    options.width ? b2d.style.widthInPixels = options.width : b2d.style.widthInPixels = 40;
    options.height ? b2d.style.heightInPixels = options.height : b2d.style.heightInPixels = 40;
    b2d.abstractGui.guiAction.addMouseLeftClickAction(()=>{})
    return b2d;
}

/**创建dom image对象
 * 
 * @param {Object3d} o3d 
 * @param {key,url,width,height,top,left} options  {key:键, url:资源路径}
 */
export function createHtmlImage(o3d,options={key:Math.random(),url:'',width:'50px',height:'50px',top:'0px',left:'0px',animated:false}){
    let div = document.createElement('div');
    let img = document.createElement('img');
    img.src = options.url;
    img.style.position = 'absolute';
    img.style.width = options.width;
    img.style.height = options.height;
    img.style.top = options.top;
    img.style.left = options.left;
    img.style.cursor = 'pointer';
    img.draggable = false;
    div.appendChild(img);
    o3d.addHtml(div,options.key);
    if(options.animated){
        let t = new Tween({y:0}).to({y:10},1000).onUpdate(obj=>{
            img.style.transform = 'translateY(' + obj.y + 'px)'
        });
        t.repeat(Infinity);
        t.yoyo(true);
        t.start();
    }
    return img;
}