import { Component, EventTouch, ImageAsset, LabelComponent, loader, Node, SpriteComponent, SpriteFrame, _decorator } from 'cc';
import { UIConst } from '../../../games/ui/UIConst';
import { LiangLiangSDK } from '../../../libs/liangliangSDK';
import { GUIManager } from '../GUIManager';
const { ccclass, property } = _decorator;

@ccclass('ExportItem')
export class ExportItem extends Component {
    
    @property({
        type:SpriteComponent,
        tooltip:"图片"
    })
    icon:SpriteComponent=null;

    @property({
        type:LabelComponent,
        tooltip:"图片"
    })
    label:LabelComponent=null;
    
    data:any;

    start () {
        this.node.on(Node.EventType.TOUCH_START,this.__itemTouchStart.bind(this),this);
    }

    updateItem(data:any):void{
        this.data=data;
        this.label.string=this.data.name;
        loader.load(this.data.icon,this.__loadProgress.bind(this),this.__loadComplete.bind(this));
    }
    
    private __loadProgress(...arg):void{
        
    }

    private __itemTouchStart(touch:EventTouch):void{
        let self = this;
        if(this.wx!=null)
        {
            this.wx.navigateToMiniProgram({
                appId: this.data.app_id,
                path: this.data.app_path,
                success: (res) => 
                {
                    LiangLiangSDK.CpaReport(self.data);
                },
                fail: (res) => {
                    GUIManager.single.Show(UIConst.FULL_OUTPUT_UI);
                    console.log("跳转失败！");
                },
                complete: (res) => {},
            });
        }
        else
        {
            console.log("点中了图标 : " + this.data.name);
        }
    }

    private get wx():any{
        return window["wx"];
    }

    private __loadComplete(err:Error,texture:ImageAsset):void{
        if(err){
            console.log(err);
            return;
        }
        let frame:SpriteFrame=new SpriteFrame();
        frame.texture=texture._texture;
        this.icon.spriteFrame=frame;
    }
    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
