import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
import { car } from '../car';
import { cheyin_item } from './cheyin_item';
import { userDataManager } from '../../manager/userDataManager';
import { audioManager } from '../../manager/audioManager';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('cheyin_manager')
export class cheyin_manager extends Component {
    @property(Prefab) cheyin_pf:Prefab = null
    private COUNT:number = 6
    private old_pos:Vec3 = Vec3.ZERO
    private new_pos:Vec3 = Vec3.ZERO
    private delay_time:number = 0.05
    private delay_count:number = 0
    private mCar:car = null
    private start_check:boolean = false
    public init(car:car){ 
        // this.mCar = car
        // this.start_check = true
        // this.old_pos = new Vec3(this.mCar.node.position.x,this.mCar.node.position.y)
        // this.node.removeAllChildren()
        // for (let index = 0; index < this.COUNT; index++) {
        //     this.addItem()
        // }
    }

    public getOldPos(){
        return this.old_pos
    }

    public getCarPos(){
        return new Vec3(this.mCar.node.position.x,this.mCar.node.position.y)
    }

    public showCheYin(){
        // if(userDataManager.user_car_list.default_car_id==1){
        //     audioManager.Instance().playSound(config.AUDIO.zi_xing_che_sha_che)
        // }else{
        //     audioManager.Instance().playSound(config.AUDIO.car_sha_che,0.1)
        // }
        // let node = this.getCanUseItem()
        // node.getComponent(cheyin_item).show()
    }

    public getCanUseItem(){
        for (let index = 0; index < this.node.children.length; index++) {
            const node = this.node.children[index];
            if(node.getComponent(cheyin_item).IsCanUse()){
                return node
            }
        }
        return this.addItem()
    }

    addItem(){
        let node = instantiate(this.cheyin_pf)
        node.parent = this.node
        node.getComponent(cheyin_item).init()
        return node
    }
    

    // protected update(dt: number): void {
        // this.node.position = new Vec3(0,this.mCar.node.parent.position.y)
        // this.delay_count+=dt;
        // if(this.delay_count>=this.delay_time){
        //     this.old_pos = new Vec3(this.mCar.node.position.x,this.mCar.node.position.y)
        //     this.delay_count = 0
        // }
        // let dis = Math.abs(this.mCar.node.position.x-this.old_pos.x)
        // if(dis>150&&this.start_check){
        //     this.delay_count = 0
        //     this.showCheYin()
        //     this.old_pos = new Vec3(this.mCar.node.position.x,this.mCar.node.position.y)
        // }
    // }
}


