package com.ejada.hmg.utils

import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import com.ejada.hmg.BuildConfig
import com.google.gson.Gson

class Logs {

    enum class STATUS{
        SUCCESS,
        ERROR;
    }
    class GeofenceEvent{
        companion object{
            fun save(context: Context, tag:String, message:String, status:Logs.STATUS = STATUS.SUCCESS){
                Logs.Common.save(context,"GeofenceEvent", tag, message, status)
            }

            fun list(context: Context, tag:String? = null, status:Logs.STATUS? = null):List<LogModel>{
                return  Logs.Common.list(context,"GeofenceEvent", tag, status)
            }

            fun raw(context: Context):String{
                return  Logs.Common.raw(context,"GeofenceEvent")
            }
        }
    }

    class RegisterGeofence{
        companion object{
            fun save(context: Context, tag:String, message:String, status:Logs.STATUS = STATUS.SUCCESS){
                Logs.Common.save(context,"RegisterGeofence", tag, message, status)
            }

            fun list(context: Context, tag:String? = null, status:Logs.STATUS? = null):List<LogModel>{
                return  Logs.Common.list(context,"RegisterGeofence", tag, status)
            }

            fun raw(context: Context):String{
                return  Logs.Common.raw(context,"RegisterGeofence");
            }
        }
    }


    companion object{
        private var pref:SharedPreferences? = null
        fun save(context: Context, tag:String, message:String, status:Logs.STATUS = STATUS.SUCCESS){
            Logs.Common.save(context,"Logs", tag, message, status)
        }

        fun list(context: Context, tag:String? = null, status:Logs.STATUS? = null):List<LogModel>{
            return  Logs.Common.list(context,"Logs", tag, status)
        }

        fun raw(context: Context):String{
            return Logs.Common.raw(context,"Logs");
        }

        private fun storage(context: Context):SharedPreferences{
            if(pref == null) {
                pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE)
            }
            return pref!!
        }
    }

    private class Common{
        companion object{
            private val gson = Gson()

            fun save(context: Context, key:String, tag:String, message:String, status:Logs.STATUS = STATUS.SUCCESS){
                if(!BuildConfig.DEBUG) 
                    return
                
                val pref = Logs.storage(context)

                val string = pref.getString(key,"{}")
                val json = gson.fromJson<LogsContainerModel>(string,LogsContainerModel::class.java)
                json.add(
                        LogModel().apply {
                            this.TAG = tag
                            this.MESSAGE = message
                            this.STATUS = status.name
                            this.DATE = DateUtils.dateTimeNow()
                        }
                )

                pref.edit().putString(key,gson.toJson(json)).apply()
            }

            fun list(context: Context, key:String, tag:String? = null, status:Logs.STATUS? = null):List<LogModel>{
                val pref = Logs.storage(context)
                val string = pref.getString(key,"{}")
                val json = gson.fromJson<LogsContainerModel>(string,LogsContainerModel::class.java)
                if(tag == null && status == null) {
                    return json.LOGS
                }else if(tag != null && status != null){
                    return json.LOGS.filter { (it.TAG == tag && it.STATUS == status.name) }
                }else if(tag != null){
                    return json.LOGS.filter { (it.TAG == tag) }
                }else if(status != null){
                    return json.LOGS.filter { (it.STATUS == status.name) }
                }
                return listOf()
            }

            fun raw(context: Context, key:String):String{
                val pref = Logs.storage(context)
                val string = pref.getString(key,"{}")
                return string!!
            }

        }
    }

    class LogModel{
        lateinit var TAG:String
        lateinit var MESSAGE:String
        lateinit var STATUS:String
        lateinit var DATE:String

        companion object{
            fun with(tag:String, message:String, status:String):LogModel{
                return LogModel().apply {
                    this.TAG = tag
                    this.MESSAGE = message
                    this.STATUS = status
                    this.DATE = DateUtils.dateTimeNow()
                }
            }
        }
    }

    class LogsContainerModel{
        var LOGS = mutableListOf<LogModel>()
        fun add(log:LogModel){
            LOGS.add(log)
        }
    }


}