<commit_before>package com.olq.whatsthatflag.screens.game

import com.olq.whatsthatflag.data.Model
import com.olq.whatsthatflag.screens.menu.MenuActivity
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

/**
 * Created by olq on 20.11.17.
 */
class GamePresenter(private val view: GameScreenContract.View,
                    private val model: Model)
    : GameScreenContract.Presenter {


    var score = 0
    var currentFlagId = 0
    var amountOfLoadedCountries = 0


    override fun start(gameData: Pair<MenuActivity.CONTINENT, Int>) {
        score = 0
        currentFlagId = 0

        view.showScore(score)

        model.selectFlags(gameData)
        amountOfLoadedCountries = model.flagList.size


        if (gameData.first == MenuActivity.CONTINENT.OCEANIA && gameData.second == 40) {
            view.displayMessage("There are only $amountOfLoadedCountries countries in Oceania")
        }

        view.setButtonsClickability(false)
        downloadImg(currentFlagId)
        renameBtns(currentFlagId)
    }

    private fun downloadImg(id: Int) {
        view.showProgressBar()

        doAsync {
            val country = model.flagList[id]
            val imgUrl = model.getImgUrl(getURLFromName(country))

            uiThread {
                when (imgUrl) {
                    null -> {
                        if (view.isConnectedToInternet()) {
                            downloadImg(id)
                            view.displayMessage("Reattempting download")
                        } else {
                            view.showNoConnectionAlert()
                        }
                    }

                    else -> {
                        view.loadImg(imgUrl)

                        view.hideProgressBar()
                        view.setButtonsClickability(true)

                        view.startAnswerTimer()
                    }
                }
            }
        }
    }

    override fun redownloadImg(goToNext: Boolean) {
        if (!goToNext) {
            downloadImg(currentFlagId)
            renameBtns(currentFlagId)

        } else {
            manageFlagId()
        }
    }

    private fun renameBtns(id: Int) {
        val btnNames = model.getButtonNames(id)
        view.renameButtons(btnNames)

        view.showRemainingQuestions(amountOfLoadedCountries - currentFlagId)
    }


    override fun answerBtnClicked(selectedCountry: String) {
        view.stopAnswerTimer()

        val correctCountry = model.flagList[currentFlagId]
        val success = isAnswerCorrect(selectedCountry, correctCountry)

        if (!success) {
            view.animateWrongAnswer(selectedCountry, correctCountry)
        } else {
            view.animateCorrectAnswer(correctCountry)
        }

        view.setButtonsClickability(false)
    }

    private fun isAnswerCorrect(selectedCountry: String, correctCountry: String): Boolean {
        if (selectedCountry == correctCountry) {
            score++
            view.showScore(score)

            return true
        }

        return false
    }

    override fun animationTimerFinished() {
        manageFlagId()
    }

    private fun manageFlagId() {
        if (currentFlagId < amountOfLoadedCountries - 1) {
            currentFlagId++
            downloadImg(currentFlagId)
            renameBtns(currentFlagId)

        } else {
            view.showRemainingQuestions(0)
            view.showSummaryDialog(score, amountOfLoadedCountries)<commit_msg>Added 'Skipped a flag' toast, refactored code for more clarity<commit_after>package com.olq.whatsthatflag.screens.game

import com.olq.whatsthatflag.data.Model
import com.olq.whatsthatflag.screens.menu.MenuActivity
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

/**
 * Created by olq on 20.11.17.
 */
class GamePresenter(private val view: GameScreenContract.View,
                    private val model: Model)
    : GameScreenContract.Presenter {


    var score = 0
    var currentFlagId = 0
    var amountOfLoadedCountries = 0


    override fun start(gameData: Pair<MenuActivity.CONTINENT, Int>) {
        score = 0
        currentFlagId = 0

        view.showScore(score)

        model.selectFlags(gameData)
        amountOfLoadedCountries = model.flagList.size


        if (gameData.first == MenuActivity.CONTINENT.OCEANIA && gameData.second == 40) {
            view.displayMessage("There are only $amountOfLoadedCountries countries in Oceania")
        }

        view.setButtonsClickability(false)
        downloadImg(currentFlagId)
        renameBtns(currentFlagId)
    }

    private fun downloadImg(id: Int) {
        view.showProgressBar()

        doAsync {
            val country = model.flagList[id]
            val imgUrl = model.getImgUrl(getURLFromName(country))

            uiThread {
                when (imgUrl) {
                    null -> {
                        if (view.isConnectedToInternet()) {
                            downloadImg(id)
                            view.displayMessage("Reattempting download")
                        } else {
                            view.showNoConnectionAlert()
                        }
                    }

                    else -> {
                        view.loadImg(imgUrl)

                        view.hideProgressBar()
                        view.setButtonsClickability(true)

                        view.startAnswerTimer()
                    }
                }
            }
        }
    }

    override fun redownloadImg(goToNext: Boolean) {
        if (!goToNext) {
            downloadImg(currentFlagId)
            renameBtns(currentFlagId)

        } else {
            view.displayMessage("Skipped a flag")
            goToNextFlag()
        }
    }

    private fun renameBtns(id: Int) {
        val btnNames = model.getButtonNames(id)
        view.renameButtons(btnNames)

        view.showRemainingQuestions(amountOfLoadedCountries - currentFlagId)
    }


    override fun answerBtnClicked(selectedCountry: String) {
        view.stopAnswerTimer()

        val correctCountry = model.flagList[currentFlagId]
        val success = isAnswerCorrect(selectedCountry, correctCountry)

        if (!success) {
            view.animateWrongAnswer(selectedCountry, correctCountry)
        } else {
            view.animateCorrectAnswer(correctCountry)
        }

        view.setButtonsClickability(false)
    }

    private fun isAnswerCorrect(selectedCountry: String, correctCountry: String): Boolean {
        if (selectedCountry == correctCountry) {
            score++
            view.showScore(score)

            return true
        }

        return false
    }

    override fun animationTimerFinished() {
        goToNextFlag()
    }

    private fun goToNextFlag() {
        if (currentFlagId < amountOfLoadedCountries - 1) {
            currentFlagId++
            downloadImg(currentFlagId)
            renameBtns(currentFlagId)

        } else {
            view.showRemainingQuestions(0)
            view.showSummaryDialog(score, amountOfLoadedCountries)
{'fraction_non_alphanumeric': 0.0773683467457292, 'fraction_numerical': 0.004235493434985175, 'mean_word_length': 1.7845911949685536, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3751570', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1823, 'n_words': 337}