import { _decorator, Collider2D, Component, Contact2DType, Director, director, instantiate, IPhysics2DContact, Node, Prefab, RigidBody2D, Size, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc';
import { model_item_data } from '../data';
import { config } from '../config';
import { box } from './box';
import { coin } from './coin';
import { wall } from './wall';
import Coroutine from '../lib/Coroutine';
import { tools } from '../tools';
import { car } from './car';
import { base } from './base';
const { ccclass, property } = _decorator;

@ccclass('run_level_item')
export class run_level_item extends Component {
    private mData:model_item_data = null;
    private sfList:SpriteFrame[] = [];
    private sfBuffList:SpriteFrame[] = [];
    private mCar:car = null;
    private isRemove:boolean = false
    public initView(data:model_item_data,l:SpriteFrame[],bl:SpriteFrame[],car:car){
        this.mData = data
        this.mCar = car
        this.sfList = l
        this.sfBuffList = bl
        this.node.getComponent(UITransform).contentSize = new Size(this.mData.width,this.mData.height)
        for (let index = 0; index < data.list.length; index++) {
            const element = data.list[index];
            // Coroutine.instance.start(this, this.init, 0.2, 0, element);
            this.init(element)
        }

    }

    private  init(element){
        let node:Node = null;
        if(tools.isBox(element.item_type)){
            node = this.mCar.getGame().getPoolManager().getBox()
            node.getComponent(box).init(element, this.sfList[element.item_type-1],element.buff_type>0?this.sfBuffList[element.buff_type-1]:null,this.mCar)
            this.mCar.getGame().addBoxForList(node)
        }else if(element.item_type==config.item_type.coin){
            node = this.mCar.getGame().getPoolManager().getCoin()
            node.getComponent(coin).init(element,this.mCar)
            this.mCar.getGame().addCoinForList(node)
        }else if(element.item_type==config.item_type.wall){
            node = this.mCar.getGame().getPoolManager().getWall()
            node.getComponent(wall).init(element,this.mCar)
            this.mCar.getGame().addWallForList(node)
        }
        if(node!=null){
            node.parent = this.node
        }
        
    }

    public removeAll(){
        // this.node.getComponent(RigidBody2D).sleep()
        for (let index = 0; index < this.node.children.length; index++) {
            const node = this.node.children[index];
            let element = node.getComponent(base).getData()
            if(tools.isBox(element.item_type)){
                node.getComponent(box).removeSelf()
            }else if(element.item_type==config.item_type.coin){
                node.getComponent(coin).removeSelf()
            }else if(element.item_type==config.item_type.wall){
                this.mCar.getGame().removeWallForList(node)
                this.mCar.getGame().getPoolManager().putWall(node)
            }
        }
        // director.once(Director.EVENT_AFTER_DRAW,()=>{
        //     this.node.destroy()
        // })
        this.node.removeFromParent()
    
    }

    public CheckRemoveSelf(){
        if(!this.isRemove){
            let world = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
            if(this.mCar.getGame().isOut(world)){
                this.isRemove = true
                this.mCar.getGame().deleteItem(this.node)
            }
        }
   
    }

    protected lateUpdate(dt: number): void {
        this.CheckRemoveSelf()
    }

}


