package bums.lunatic.launcher.view

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.provider.AlarmClock
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import java.text.SimpleDateFormat
import java.util.Date


class DateTimeView : AppCompatTextView {
    lateinit var mHandler : Handler
    val simpleTimeFormat = SimpleDateFormat("a HH:mm:ss")
    val simpleDateFormat = SimpleDateFormat("yyyy년 MM월 W주차 dd일 E요일")
    var runable = {
        setTime()
    }
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {

    }

    init {
        mHandler = Handler(Looper.getMainLooper())
        mHandler.postDelayed(runable, DateTimeView.DALEY)
    }

    val spannableBuilder = SpannableStringBuilder()

    private fun setTime() {
        spannableBuilder.clear()
        val now = Date(System.currentTimeMillis())
        val time = SpannableString(simpleTimeFormat.format(now))
        val date = SpannableString(simpleDateFormat.format(now))
        val mClickableSpan = object: ClickableSpan(){
            override fun updateDrawState(textPaint: TextPaint) {
//                textPaint.color = this@MainActivity.getColor(R.color.blue)
                textPaint.isUnderlineText = false
            }

            override fun onClick(v: View) {
                val mClockIntent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
                mClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                context.startActivity(mClockIntent)
            }
        }
        val mClickableSpan2 = object: ClickableSpan(){
            override fun updateDrawState(textPaint: TextPaint) {
//                textPaint.color = this@MainActivity.getColor(R.color.blue)
                textPaint.isUnderlineText = false
            }

            override fun onClick(v: View) {
                val intent = Intent(Intent.ACTION_MAIN)
                intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
                context.startActivity(intent)
            }
        }

        spannableBuilder.append(time)
        spannableBuilder.setSpan(AbsoluteSizeSpan(48,true), 0, time.length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableBuilder.setSpan(mClickableSpan, 0, time.length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableBuilder.append("\n")
        spannableBuilder.append(date)
        var start = time.length + "\n".length
        spannableBuilder.setSpan(AbsoluteSizeSpan(30,true), start , start+date.length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableBuilder.setSpan(mClickableSpan2, start , start+date.length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        mHandler.removeCallbacks(runable)
        setText(spannableBuilder)
        mHandler.postDelayed(runable, DateTimeView.DALEY)
        setMovementMethod(LinkMovementMethod.getInstance());

    }

    companion object {
        const val DALEY = 500L
    }
}