package rasel.lunar.launcher.workers

import android.annotation.SuppressLint
import android.content.Context
import android.provider.CallLog
import androidx.work.WorkerParameters
import com.google.gson.Gson
import rasel.lunar.launcher.LauncherActivity.Companion.lActivity
import rasel.lunar.launcher.home.LauncherHome.Companion.recentCalls
import rasel.lunar.launcher.utils.beforeDay
import java.text.SimpleDateFormat
import java.util.Date



class RecentCall {
    constructor(count: Int, name: String?, number: String, type: Int, typeString: String, date : String) {
        this.count = count
        this.name = name ?: "unknown"
        this.number = number
        this.type = type
        this.typeString = typeString
        this.date = date
    }

    var count : Int = 1
    var name : String = "how"
    var number : String = ""
    var type : Int = 0
    var typeString : String = ""
    var date : String = ""

    fun toJson() : String {
        return Gson().toJson(this)
    }

}

class RecentCallGetter : BaseGetter {

    constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams) {
    }

    @SuppressLint("RestrictedApi")
    override fun doWork(): Result {

        var dateParam = beforeDay(Date(),3).toString()
        var managedCursor = lActivity?.contentResolver?.query(
            CallLog.Calls.CONTENT_URI, arrayOf(
                CallLog.Calls.NUMBER,
                CallLog.Calls.TYPE,
                CallLog.Calls.DATE,
                CallLog.Calls.DURATION,
                CallLog.Calls.CACHED_NAME,
            ), CallLog.Calls.DATE + " >= ? " ,  arrayOf<String>(dateParam),  CallLog.Calls.DATE + " desc")
        //+ CallLog.Calls.TYPE + " > ?"
        //, "2"
        if(managedCursor != null && managedCursor.isClosed == false) {
            try {
                val number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER)
                val type = managedCursor.getColumnIndex(CallLog.Calls.TYPE)
                val date = managedCursor.getColumnIndex(CallLog.Calls.DATE)
                val duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION)
                val name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME)
                recentCalls.clear()
                while (managedCursor.moveToNext()) {
                    val phNumber = managedCursor.getString(number) // mobile number
                    val callType = managedCursor.getString(type) // call type
                    val callDate = managedCursor.getString(date) // call date
                    val callDayTime: Date = Date(callDate.toLong())
                    val callDuration = managedCursor.getString(duration)
                    val callerName = managedCursor.getString(name)

                    var dir: String = ""
                    val dircode = callType.toInt()
                    when (dircode) {
                        CallLog.Calls.INCOMING_TYPE -> {
                            dir = "INCOMING_TYPE"
                        }

                        CallLog.Calls.OUTGOING_TYPE -> {
                            dir = "OUTGOING_TYPE"
                        }

                        CallLog.Calls.MISSED_TYPE -> {
                            dir = "MISSED_TYPE"
                        }

                        CallLog.Calls.VOICEMAIL_TYPE -> {
                            dir = "VOICEMAIL_TYPE"
                        }

                        CallLog.Calls.REJECTED_TYPE -> {
                            dir = "REJECTED_TYPE"
                        }

                        CallLog.Calls.BLOCKED_TYPE -> {
                            dir = "BLOCKED_TYPE"
                        }

                        CallLog.Calls.ANSWERED_EXTERNALLY_TYPE -> {
                            dir = "ANSWERED_EXTERNALLY_TYPE"
                        }
                    }
                    var missed: RecentCall = if (recentCalls.containsKey(phNumber)) {
                        recentCalls.get(phNumber)!!.apply {
                            count = count + 1
                        }
                    } else {
                        RecentCall(
                            1,
                            callerName,
                            phNumber,
                            dircode,
                            dir,
                            SimpleDateFormat("yyy/MM/dd-HH:mm:ss").format(callDayTime)
                        )
                    }
//                    BLog.LOGE("missed put >>> ${missed.toJson()}")
                    recentCalls.put(phNumber, missed)
                }




            } catch (e: Exception) {

            } finally {
                managedCursor.close()
            }
        }
        return Result.success()
    }

}