package com.gh.ndownload.suspendwindow.view

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieDrawable
import com.gh.gamecenter.R

class NDownloadDrawOverlayPermissionLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : LinearLayout(context, attrs, defStyle) {

    init {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
    }

    val root = FrameLayout(context).also {
        it.setBackgroundResource(R.drawable.shape_download_draw_overlay_permission)
        val layoutParams = LayoutParams(
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                200F,
                context.resources.displayMetrics
            ).toInt(),
            LayoutParams.WRAP_CONTENT
        )
        addView(it, layoutParams)
    }

    val arrowRight = ImageView(context).also {
        it.setImageResource(R.drawable.ic_download_draw_overlay_permission_arrow)
        val layoutParams = LayoutParams(
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                14F,
                context.resources.displayMetrics
            ).toInt(),
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                24F,
                context.resources.displayMetrics
            ).toInt()
        )
        it.scaleType = ImageView.ScaleType.FIT_XY
        addView(it, layoutParams)
    }

    val close = ImageView(context).also {
        val size = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            16F,
            context.resources.displayMetrics
        ).toInt()
        it.setImageResource(R.drawable.ic_download_draw_overlay_permission_close)
        val layoutParams = FrameLayout.LayoutParams(size, size).apply {
            val margin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                8F,
                context.resources.displayMetrics
            ).toInt()
            gravity = Gravity.END or Gravity.TOP
            topMargin = margin
            rightMargin = margin
        }
        root.addView(it, layoutParams)
    }


    val content = LinearLayout(context).also {
        val layoutParams = FrameLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT
        )
        it.gravity = Gravity.CENTER_HORIZONTAL
        it.orientation = VERTICAL
        root.addView(it, layoutParams)
    }

    val title = TextView(context).also {
        it.textSize = 12F
        it.gravity = Gravity.CENTER
        it.text = context.getString(R.string.download_suspend_draw_overlay_permission_title)
        it.setLineSpacing(4F, 1F)
        it.includeFontPadding = false
        it.setTextColor(ContextCompat.getColor(context, android.R.color.white))
        val layoutParams = LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT,
        ).apply {
            topMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 12F, resources.displayMetrics
            ).toInt()
        }
        content.addView(it, layoutParams)
    }

    val lottie = LottieAnimationView(context).also {
        it.setImageResource(R.drawable.ic_download_draw_overlay_permission_lottie)
        it.setAnimation("lottie/draw_overlay_permission.json")
        it.repeatCount = LottieDrawable.INFINITE
        it.playAnimation()
        it.setBackgroundResource(R.drawable.shape_download_draw_overlay_permission_lottie)
        val padding = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            4F,
            resources.displayMetrics
        ).toInt()
        it.setPadding(padding, padding, padding, padding)
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                112F,
                context.resources.displayMetrics
            ).toInt(),
        ).apply {
            topMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                8F,
               resources.displayMetrics
            ).toInt()
            leftMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                12F,
                resources.displayMetrics
            ).toInt()
            rightMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                12F,
                resources.displayMetrics
            ).toInt()
        }
        content.addView(it, layoutParams)
    }

    val button = TextView(context).also {
        it.textSize = 14F
        it.gravity = Gravity.CENTER
        it.text = context.getString(R.string.download_suspend_draw_overlay_permission_confirm)
        it.includeFontPadding = false
        it.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
        it.setBackgroundResource(R.drawable.shape_download_draw_overlay_permission_button)
        it.setTextColor(Color.parseColor("#1383EB"))
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 28F, resources.displayMetrics
            ).toInt(),
        ).apply {
            val otherMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 12F, resources.displayMetrics
            ).toInt()
            val tMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 8F, resources.displayMetrics
            ).toInt()
            topMargin = tMargin
            leftMargin = otherMargin
            rightMargin = otherMargin
            bottomMargin = otherMargin
        }
        content.addView(it, layoutParams)
    }
}