import { Vec3, v3 } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { MoveToComp } from "../../common/ecs/position/MoveTo";
import { Subway } from "../Subway";
import { SubwayModelComp } from "../model/SubwayModelComp";
import { smc } from "../../common/SingletonModuleComp";
import { SubwayEnum } from "../model/SubwayEnum";
import { Puppet } from "../../puppet/puppet";
import { PuppetModelComp } from "../../puppet/model/PuppetModelComp";
import { PuppetAnimatorType } from "../../puppet/model/PuppetEnum";
import { PathFindComp } from "../../common/ecs/path/PathFind";
import { CheckpointPathTriggerComp } from "../../checkpoint/bll/CheckpointPathTrigger";


/** 检查左侧和右侧是否可以出人 */
@ecs.register('SubwayGenPuppet')
export class SubwayGenPuppetComp extends ecs.Comp {
    isGen:boolean = false
    reset() {
        this.isGen = false
     }
}

export class SubwayGenPuppetSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
    filter(): ecs.IMatcher {
        return ecs.allOf(SubwayGenPuppetComp, SubwayModelComp);
    }

    // entityEnter(e: Subway): void {

    //     e.remove(SubwayGenPuppetComp)
    // }

    update(e: Subway): void {
        const checkpoint = smc.initialize.account.checkpoint
        const genComp = e.get(SubwayGenPuppetComp)
        if(genComp.isGen||e.SubwayModel.colorArr.length===0)return
        if (checkpoint && checkpoint.CheckpointModel) {
            const cells = checkpoint.CheckpointModel.cells
            const grids = checkpoint.CheckpointModel.grids
            if (e.SubwayModel.type === SubwayEnum.LEFT) {
                if (e.SubwayModel.y === 0) {
                    console.log('地铁靠边出不来的哦')
                    return
                }
                const cell = cells[e.SubwayModel.x][e.SubwayModel.y-1]
                const grid = grids[e.SubwayModel.x][e.SubwayModel.y-1]
                if (grid && !cell) {
                    genComp.isGen = true
                    const pos = grid.GridView.node.position.clone()
                    this.createPuppet(e, e.SubwayModel.x, e.SubwayModel.y-1, pos)
                }
            } else {
                if (e.SubwayModel.y === cells[0]?.length - 1) {
                    console.log('地铁靠边出不来的哦')
                    return
                }
                const cell = cells[e.SubwayModel.x][e.SubwayModel.y+1]
                const grid = grids[e.SubwayModel.x][e.SubwayModel.y+1]
                if (grid && !cell) {
                    genComp.isGen = true
                    const pos = grid.GridView.node.position.clone()
                    this.createPuppet(e, e.SubwayModel.x, e.SubwayModel.y + 1, pos)
                }
            }

        }
    }

    createPuppet(e: Subway, x: number, y: number, end: Vec3) {
        const genComp = e.get(SubwayGenPuppetComp)
        if (!end|| !e.SubwayView.node) {
            genComp.isGen = false
            return
        }
        const account = smc.initialize.account
        const puppet = ecs.getEntity<Puppet>(Puppet)
        const puppetModel = puppet.get(PuppetModelComp)
        const color = e.SubwayModel.colorArr.shift()
        e.SubwayView.updateCount(e.SubwayModel.colorArr.length)
        puppetModel.color = color
        puppetModel.withColider = true
        puppet.load(account.checkpoint.CheckpointView.node, color, e.SubwayView.node.position.clone(), account.AccountModel.skin);
        e.addChild(puppet)
        if (e.SubwayModel.type === SubwayEnum.LEFT) {
            puppet.PuppetView.animator.toLeft()
        } else {
            puppet.PuppetView.animator.toRight()
        }
        puppet.PuppetView.animator.playAni(PuppetAnimatorType.Run)
        puppet.PuppetView.animator.scale()
        puppet.PuppetView.animator.onRunComplete = () => {
            puppet.PuppetView.animator.toStation()
        }
        puppet.PuppetView.animator.moveToTarget(end)
        
        if (e.SubwayModel.type === SubwayEnum.LEFT) {
            account.checkpoint.CheckpointModel.cells[e.SubwayModel.x][e.SubwayModel.y - 1] = puppet
            account.checkpoint.CheckpointModel.path_grid[e.SubwayModel.x][e.SubwayModel.y - 1] = {
                x: e.SubwayModel.x,
                y: e.SubwayModel.y - 1,
                fill: color,
                pos: end
            }
            puppetModel.x =  e.SubwayModel.x
            puppetModel.y =  e.SubwayModel.y-1
            const pathComp = puppet.add(PathFindComp)
            pathComp.x = e.SubwayModel.x
            pathComp.y = e.SubwayModel.y - 1
        } else {
            account.checkpoint.CheckpointModel.cells[e.SubwayModel.x][e.SubwayModel.y + 1] = puppet
            account.checkpoint.CheckpointModel.path_grid[e.SubwayModel.x][e.SubwayModel.y + 1] = {
                x: e.SubwayModel.x,
                y: e.SubwayModel.y + 1,
                fill: color,
                pos: end
            }
            puppetModel.x =  e.SubwayModel.x
            puppetModel.y =  e.SubwayModel.y+1
            const pathComp = puppet.add(PathFindComp)
            pathComp.x = e.SubwayModel.x
            pathComp.y = e.SubwayModel.y + 1
        }
        account.checkpoint.add(CheckpointPathTriggerComp)
        genComp.isGen = false
    }
}