package rasel.lunar.launcher.workers

import android.content.Context
import android.net.Uri
import androidx.work.WorkerParameters
import rasel.lunar.launcher.LauncherActivity.Companion.lActivity
import rasel.lunar.launcher.utils.BLog


class CalendarGetter : BaseGetter {
    companion object {
        val TAG = "DCGetter"
    }
    constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams) {

    }
    override fun realWork(): Result {
        setCalendar()
        return Result.success().apply {

        }
    }

    fun setCalendar() {
 

        val calendars = Uri.parse("content://com.android.calendar/events")

        val projection = arrayOf(
            "calendar_id",
//            "htmlUri",
            "title",
//            "eventLocation",
            "description",
//            "eventStatus",
//            "selfAttendeeStatus",
//            "commentsUri",
            "dtstart",
            "dtend",
//            "eventTimezone",
//            "duration",
//            "allDay",
//            "visibility",
//            "transparency",
//            "hasAlarm",
//            "hasExtendedProperties",
//            "rrule",
            "rdate",
//            "exrule",
//            "exdate",
//            "originalEvent",
//            "originalInstanceTime",
//            "originalAllDay",
//            "lastDate",
//            "hasAttendeeData",
//            "guestsCanModify",
//            "guestsCanInviteOthers",
//            "guestsCanSeeGuests",
//            "organizer",
//            "deleted"
        )
//        val managedCursor: Cursor =
        lActivity?.contentResolver?.query(calendars, projection, null, null, null)?.let { managedCursor ->
            if (managedCursor.moveToFirst()) {
                val calendar_id = IntArray(managedCursor.count)

//                val htmlUri = arrayOfNulls<String>(managedCursor.count)
                val title = arrayOfNulls<String>(managedCursor.count)
//                val eventLocation = arrayOfNulls<String>(managedCursor.count)
                val description = arrayOfNulls<String>(managedCursor.count)
//                val eventStatus = IntArray(managedCursor.count)
//                val selfAttendeeStatus = IntArray(managedCursor.count)
//                val commentsUri = arrayOfNulls<String>(managedCursor.count)
                val dtstart = arrayOfNulls<String>(managedCursor.count)
                val dtend = arrayOfNulls<String>(managedCursor.count)
//                val eventTimezone = arrayOfNulls<String>(managedCursor.count)
//                val duration = arrayOfNulls<String>(managedCursor.count)
//                val allDay = IntArray(managedCursor.count)
//                val visibility = IntArray(managedCursor.count)
//                val transparency = IntArray(managedCursor.count)
//                val hasAlarm = IntArray(managedCursor.count)
//                val hasExtendedProperties = IntArray(managedCursor.count)
//                val rrule = arrayOfNulls<String>(managedCursor.count)
                val rdate = arrayOfNulls<String>(managedCursor.count)
//                val exrule = arrayOfNulls<String>(managedCursor.count)
//                val exdate = arrayOfNulls<String>(managedCursor.count)
//                val originalEvent = arrayOfNulls<String>(managedCursor.count)
//                val originalInstanceTime = IntArray(managedCursor.count)
//                val originalAllDay = IntArray(managedCursor.count)
//                val lastDate = IntArray(managedCursor.count)
//                val hasAttendeeData = IntArray(managedCursor.count)
//                val guestsCanModify = IntArray(managedCursor.count)
//                val guestsCanInviteOthers = IntArray(managedCursor.count)
//                val guestsCanSeeGuests = IntArray(managedCursor.count)
//                val organizer = arrayOfNulls<String>(managedCursor.count)
//                val deleted = IntArray(managedCursor.count)

                for (i in title.indices) {
                    calendar_id[i] = managedCursor.getInt(0)
                    BLog.LOGE("Calendar ID : " + calendar_id[i])
//                    htmlUri[i] = managedCursor.getString(1)
//                    Log.i("Calendar", "htmlUri : " + htmlUri[i])
                    title[i] = managedCursor.getString(1)
                    BLog.LOGE("Calendar title : " + title[i])
//                    eventLocation[i] = managedCursor.getString(3)
//                    Log.i("Calendar", "eventLocation : " + eventLocation[i])
                    description[i] = managedCursor.getString(2)
//                    eventStatus[i] = managedCursor.getInt(5)
//                    selfAttendeeStatus[i] = managedCursor.getInt(6)
//                    commentsUri[i] = managedCursor.getString(7)
                    dtstart[i] = managedCursor.getString(3)
                    BLog.LOGE("Calendar dtstart : " + rdate[i])
                    dtend[i] = managedCursor.getString(4)
                    BLog.LOGE("Calendar dtend : " + rdate[i])
//                    eventTimezone[i] = managedCursor.getString(10)
//                    duration[i] = managedCursor.getString(11)
//                    allDay[i] = managedCursor.getInt(12)
//                    visibility[i] = managedCursor.getInt(13)
//                    transparency[i] = managedCursor.getInt(14)
//                    hasAlarm[i] = managedCursor.getInt(15)
//                    hasExtendedProperties[i] = managedCursor.getInt(16)
//                    rrule[i] = managedCursor.getString(17)
                    rdate[i] = managedCursor.getString(5)
                    BLog.LOGE("Calendar rdate : " + rdate[i])
//                    exrule[i] = managedCursor.getString(19)
//                    exdate[i] = managedCursor.getString(20)
//                    originalEvent[i] = managedCursor.getString(21)
//                    originalInstanceTime[i] = managedCursor.getInt(22)
//                    originalAllDay[i] = managedCursor.getInt(23)
//                    lastDate[i] = managedCursor.getInt(24)
//                    hasAttendeeData[i] = managedCursor.getInt(25)
//                    guestsCanModify[i] = managedCursor.getInt(26)
//                    guestsCanInviteOthers[i] = managedCursor.getInt(27)
//                    guestsCanSeeGuests[i] = managedCursor.getInt(28)
//                    organizer[i] = managedCursor.getString(29)
//                    deleted[i] = managedCursor.getInt(30)

                    if (title[i] != null) {
                        BLog.LOGE("title[i] ${title[i]}")
                    }

                    managedCursor.moveToNext()
                }
            }
            managedCursor.close()
        }
    }

}