package me.msoucy.ptures.view

import me.msoucy.ptures.model.Creature
import me.msoucy.ptures.model.Skill
import me.msoucy.ptures.model.SkillChoice
import me.msoucy.ptures.model.VisibleStatus

class SkillViewText(skill: Skill) : SkillView(skill) {
    override fun display() {
        println(skill.name)
    }

    override fun displayEnumerated(idx: Int) {
        println("${idx + 1}: ${skill.name}")
    }
}

class CreatureViewText(playerId: Int, creature: Creature) : CreatureView(playerId, creature) {

    private val skillViews = creature.skills.map { SkillViewText(it) }

    private fun chooseSkillName(): Skill {
        println("Skills:")
        println("=======")
        skillViews.forEachIndexed { index, skillView ->
            skillView.displayEnumerated(index)
        }
        var idx = -1
        while (idx != -1) {
            print("> ")
            val tmpIdx = readLine()?.toIntOrNull() ?: -1
            if (tmpIdx in creature.skills.indices) {
                idx = tmpIdx
            }
        }
        return creature.skills[idx]
    }

    private fun chooseTarget(skill: Skill, possibleTargets: List<Creature>): List<Creature> {
        return possibleTargets
    }

    override fun chooseSkill(possibleTargets: List<Creature>) {

        if (creature.activeSkill == null) {
            val skill = chooseSkillName()
            val targets = chooseTarget(skill, possibleTargets)
            creature.activeSkill = SkillChoice(skill, targets)
        }
    }

    override fun displayName() {
        println(creature.name)
    }

    override fun displaySkills() {
        skillViews.forEach { skillView ->
            skillView.display()
        }
    }

    override fun displayStatuses() {
        creature.statuses.filterIsInstance<VisibleStatus>().forEach {
            println(it.label)
        }
    }
}

class PlayerViewText(playerId: Int) : PlayerView(playerId) {
    override fun creatureViewFor(creature: Creature) = CreatureViewText(playerId, creature)
}