package com.example.randomtaskmobile

import GlobalSharedPreferences
import ReciclerViewAdapter
import TaskStorage
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.room.Room
import com.example.randomtaskmobile.databinding.ActivityListTaskBinding
import model.AppDatabase
import model.TaskDao
import model.TaskEntity

class ListTaskActivity : AppCompatActivity(), ReciclerViewAdapter.ListenerAdapter {
    private  lateinit var shPref: GlobalSharedPreferences
    private lateinit var binding: ActivityListTaskBinding
    private lateinit var db : AppDatabase
    private lateinit var taskDao : TaskDao

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityListTaskBinding.inflate(layoutInflater)
        setContentView(binding.root)
        shPref = GlobalSharedPreferences(this)

        if(shPref.firstStartApp)
            shPref.firstStartApp = false

        db = AppDatabase.getInstance(applicationContext)
        taskDao = db.taskDao()

        viewNotCompleteTask()
        listener()
    }

    private fun listener(){
        with(binding){
            actualTask.setOnClickListener(){
                completeTask.setTextColor(getColor(R.color.gray))
                actualTask.setTextColor(getColor(R.color.olive))
                viewNotCompleteTask()
            }

            completeTask.setOnClickListener(){
                completeTask.setTextColor(getColor(R.color.olive))
                actualTask.setTextColor(getColor(R.color.gray))
                viewCompleteTask()
            }

            generateButtonOnList.setOnClickListener(){
                var intent = Intent(this@ListTaskActivity, GenerateTaskActivity::class.java)
                intent.putExtra("work", "create")
                startActivity(intent)
            }
        }
    }

    private fun viewCompleteTask(){
        TaskStorage.listItem = taskDao.getAllComplete()
        if(TaskStorage.listItem.size <= 0){
            with(binding){
                listTask.visibility = View.GONE
                textIfEmpty.visibility = View.VISIBLE
            }
        }
        else{
            with(binding){
                listTask.visibility = View.VISIBLE
                textIfEmpty.visibility = View.GONE
            }
            setRecyclerView(TaskStorage.listItem)
        }
    }

    private fun viewNotCompleteTask(){
        TaskStorage.listItem= taskDao.getAllNotComplete()
        if(TaskStorage.listItem.size <= 0){
            with(binding){
                listTask.visibility = View.GONE
                textIfEmpty.visibility = View.VISIBLE
            }
        }
        else{
            with(binding){
                listTask.visibility = View.VISIBLE
                textIfEmpty.visibility = View.GONE
            }
            setRecyclerView(TaskStorage.listItem)
        }
    }

    private fun setRecyclerView(itemCollection : MutableList<TaskEntity>)
    {
        with(binding)
        {
            listTask.layoutManager = LinearLayoutManager(this@ListTaskActivity, LinearLayoutManager.VERTICAL, false)
            listTask.adapter = ReciclerViewAdapter(itemCollection, this@ListTaskActivity)
        }
    }

    override fun setComplete(taskEntity: TaskEntity) {
        taskEntity.complete = !taskEntity.complete
        taskDao.updateTasks(taskEntity)
        if(!taskEntity.complete){
            viewCompleteTask()
        }
        else{
            viewNotCompleteTask()
        }
    }

    override fun update(taskEntity: TaskEntity) {
        var intent = Intent(this, GenerateTaskActivity::class.java)
        intent.putExtra("work", "update")
        intent.putExtra("idTask", taskEntity.id)
        startActivity(intent)
    }

    override fun delete(taskEntity: TaskEntity) {
        taskDao.delete(taskEntity)
        if(taskEntity.complete){
            viewCompleteTask()
        }
        else{
            viewNotCompleteTask()
        }
    }
}