//
//  EntityManager.swift
//  GoldWars
//
//  Created by Aldin Duraki on 01.05.20.
//  Copyright © 2020 SP2. All rights reserved.
//

import Foundation
import SpriteKit
import GameplayKit

class EntityManager {
    
    var entities = Set<GKEntity>()
    let scene: SKScene
	var isModal:Bool
    
    init(scene: SKScene) {
        self.scene = scene
		isModal = false
    }
    
    func add(_ entity: GKEntity) {
        entities.insert(entity)
		if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode {
				scene.addChild(spriteNode)
		}
		if let fire = entity.component(ofType: TeamComponent.self)?.fire{
				scene.addChild(fire)
		}
		if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
				scene.addChild(spriteNode)
				isModal = true
		}
		if let modal = entity.component(ofType: ModalContentComponent.self) {
			scene.addChild(modal.header)
			scene.addChild(modal.body)
			scene.addChild(modal.footer)
		}
		if let skill = entity.component(ofType: AtkBoostSkillComponent.self) {
			scene.addChild(skill.shapeNode)
			scene.addChild(skill.labelNode)
		}
		if let skill = entity.component(ofType: DefBoostSkillComponent.self) {
			scene.addChild(skill.shapeNode)
			scene.addChild(skill.labelNode)
		}
		if let skill = entity.component(ofType: SpySkillComponent.self) {
			scene.addChild(skill.shapeNode)
			scene.addChild(skill.labelNode)
		}
        if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode {
            scene.addChild(buttonNode)
        }
        if let nodes = entity.component(ofType: BackgroundComponent.self)?.nodes {
            for node in nodes {
                scene.addChild(node)
            }
        }


    }
    
    func remove(_ entity: GKEntity) {
        if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode {
            spriteNode.removeFromParent()
        }
		if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
			spriteNode.removeFromParent()
				isModal = false
		}
		if let modal = entity.component(ofType: ModalContentComponent.self) {
			modal.header.removeFromParent()
			modal.body.removeFromParent()
			modal.footer.removeFromParent()
		}
        entities.remove(entity)
    }

	func getBaseByTeam(for team: Team) -> GKEntity? {
		for entity in entities {
			if let teamComponent = entity.component(ofType: TeamComponent.self),
				let _ = entity.component(ofType: DefaultBaseComponent.self) {
                if teamComponent.team == team {
                    return entity
				}
			}
		}
		return nil
	}

    func getBackground() -> GKEntity? {
        return entities.filter{$0 is Background}[0]
    }

	func getBaseNodeByTeam(for team: Team) -> SKSpriteNode?{
		return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode
	}

    func getButtonByName(buttonName:String) -> Button{
        return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button
    }
}
