package com.birdsounds.identify.presentation

import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import java.time.Instant


class AScore(
    val _species: String,
    val _score: Float,
    val _timestamp: Long,
    val _new_entry: Boolean = false,
    val _trigger: Boolean = false,
    val _redraw_number: Long = 0,
) {
    val split_stuff: List<String> = _species.split("_");
    val species = split_stuff[0];
    val score = _score;
    var trigger = _trigger;
    var new_entry = _new_entry;

    val common_name = if (split_stuff.size > 1) split_stuff[1] else "";
    val timestamp = _timestamp

    fun age(): Long {
        var tstamp: Long = Instant.now().toEpochMilli()
        return (tstamp - timestamp)
    }


    override fun toString(): String {
        var tstamp: Long = Instant.now().toEpochMilli()
        return common_name + "," + species + "," + score.toString() + ", " + (age() / 1000.0).toString() + "s ago"
    }
}

var internal_list = mutableListOf<AScore>()

object SpeciesList {
    lateinit var _mainState: MainState
    var num_new_entries = 0;
    var _list_on_ui: SnapshotStateList<MutableState<AScore>>? = null

    fun setSpeciecsShow_list(list_in: SnapshotStateList<MutableState<AScore>>) {
        _list_on_ui = list_in;
    }

    fun clear_new_flags() {
        for (i in internal_list) {
            i.new_entry = false;
        }
    }
    
    fun add_observation(species_in: AScore): Boolean {

        var idx = 0
        var idx_replace = -1
        var added_new = false;
        for (i in internal_list) {
            if (i.species == species_in.species) {
                idx_replace = idx
            }
            idx += 1
        }
        if (idx_replace >= 0) {
            internal_list[idx_replace] = species_in
            internal_list[idx_replace].trigger = true;
        } else {
            internal_list.add(species_in)
            added_new = true;
        }

//        internal_list = internal_list.sortedBy({ (it.age()) }).toMutableList()
        internal_list =
            internal_list.sortedWith(compareBy({ it.age() }, { it.score })).toMutableList()

        return added_new

    }



    fun update_list_on_ui_thread(
    ) {
//        _list_on_ui!!.clear();
        while (_list_on_ui!!.size < internal_list.size) {
            _list_on_ui!!.add(mutableStateOf(AScore("", 0.0F, 0L)))
        }
        val time_stamp = System.currentTimeMillis()
        for ((index, value) in internal_list.withIndex()) {
            _list_on_ui?.get(index)?.value = value;
        }
    }

    fun bird_not_heard() {
        _mainState.birdHeardState= BirdHeardState.NotHearing;
        Log.d(TAG, "I set the state to not hearing")
    }

    fun bird_heard() {
        _mainState.birdHeardState = BirdHeardState.Hearing;
        Log.d(TAG, "I set the state to hearing")
    }

    fun setBirdHeardVar(birdHeardState: BirdHeardState) {

    }

    fun setMainState(mainState: MainState) {
        _mainState = mainState;
        Log.d(TAG, "I set the internal app main state variable")
    }
}