package cn.muchinfo.rma.view.autoWidget

import android.app.Activity
import android.graphics.Color
import android.graphics.Typeface

import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.DrawableCompat
import cn.muchinfo.rma.R
import com.qmuiteam.qmui.util.QMUIResHelper
import com.qmuiteam.qmui.widget.QMUITopBar

import org.jetbrains.anko.*
import java.util.concurrent.TimeUnit


fun QMUITopBar.commonLeftButton(onClick: ((View?) -> Unit)? = null): Int = leftButton(onClick = onClick)

fun QMUITopBar.commonLeftButtonTwo(onClick: ((View?) -> Unit)? = null): Int = leftButton(onClick = onClick,imageSize = autoSize(30))

fun QMUITopBar.leftButton(imageResource: Int = R.mipmap.back,
                          buttonWidth: Int? = null,
                          imageSize: Int = autoSize(48),
                          showDivider: Boolean = false,
                          onClick: ((View?) -> Unit)? = null): Int {
    val topBarHeight = autoSize(QMUIResHelper.getAttrDimen(context, R.attr.qmui_topbar_height))
    val topBarMainColor = Color.WHITE
    val drawable = AppCompatResources.getDrawable(context, imageResource)?.let {
        val mutate = it.mutate()
        DrawableCompat.setTint(mutate, topBarMainColor)
        mutate
    }
    val leftButton = _FrameLayout(context).apply {
        imageView {
            if (drawable == null) setImageResource(imageResource) else setImageDrawable(drawable)
            scaleType = ImageView.ScaleType.FIT_CENTER
        }.lparams(imageSize, imageSize) {
            gravity = Gravity.CENTER
        }
        if (showDivider) {
            view {
                backgroundColor = topBarMainColor
            }.lparams(autoSize(1), autoSize(48)) {
                gravity = Gravity.END.or(Gravity.CENTER_VERTICAL)
            }
        }
        onThrottleFirstClick { onClick?.invoke(it) ?: (context as Activity).finish() }
    }
    addLeftView(leftButton, R.id.qmui_topbar_item_left_back, RelativeLayout.LayoutParams(buttonWidth
            ?: topBarHeight, topBarHeight))
    return R.id.qmui_topbar_item_left_back
}

fun QMUITopBar.leftButtons(showDivider: Boolean = true,
                           block: _LinearLayout.(topBarHeight: Int, btnSrcSize: Int, topBarMainColor: Int) -> Unit): Int {
    val topBarHeight = autoSize(QMUIResHelper.getAttrDimen(context, R.attr.qmui_topbar_height))
    val topBarMainColor = Color.WHITE
    val leftLayout = _LinearLayout(context).apply {
        orientation = LinearLayout.HORIZONTAL
        block.invoke(this, topBarHeight, autoSize(48), topBarMainColor)
        if (showDivider) {
            view {
                backgroundColor = topBarMainColor
            }.lparams(autoSize(1), autoSize(48)) {
                gravity = Gravity.END.or(Gravity.CENTER_VERTICAL)
            }
        }
    }
    addLeftView(leftLayout, R.id.qmui_topbar_item_left_back, RelativeLayout.LayoutParams(wrapContent, topBarHeight))
    return R.id.qmui_topbar_item_left_back
}

/**
 * 通用的右侧menu按键
 */
fun QMUITopBar.commonMenuButton(imageResource: Int, width: Int = autoSize(48), height: Int = autoSize(48), onClick: ((View?) -> Unit)): Int {
    val topBarHeight = QMUIResHelper.getAttrDimen(context, R.attr.qmui_topbar_height)
    val topBarMainColor = Color.WHITE
    val drawable = AppCompatResources.getDrawable(context, imageResource)?.let {
        val mutate = it.mutate()
//        DrawableCompat.setTint(mutate, topBarMainColor)
        mutate
    }
    val rightButton = _FrameLayout(context).apply {
        imageView {
            setImageDrawable(drawable)
            scaleType = ImageView.ScaleType.FIT_CENTER
        }.lparams(width, height) {
            gravity = Gravity.CENTER
        }
        onThrottleFirstClick(500, TimeUnit.MICROSECONDS, onClick)
    }
    addRightView(rightButton, R.id.top_bar_right_button, RelativeLayout.LayoutParams(topBarHeight, topBarHeight))
    return R.id.top_bar_right_button
}

/**
 * 通用的右侧文字按钮
 */
fun QMUITopBar.commonTextButton(text: String, init: (TextView.() -> Unit)? = null, onClick: ((View?) -> Unit)): Int {
    val topBarHeight = QMUIResHelper.getAttrDimen(context, R.attr.qmui_topbar_height)
    val rightButton = _FrameLayout(context).apply {
        textView {
            setPadding(autoSize(32), 0, autoSize(32), 0)
            id = R.id.top_bar_right_text
            this.text = text
            textColor = Color.WHITE
            textSizeAuto = 36
            init?.invoke(this)
            onThrottleFirstClick(500, TimeUnit.MICROSECONDS, onClick)

        }.lparams(wrapContent, wrapContent) {
            gravity = Gravity.CENTER
        }
    }
    addRightView(rightButton, R.id.top_bar_right_button, RelativeLayout.LayoutParams(wrapContent, topBarHeight))
    return R.id.top_bar_right_button
}

/**
 * 通用的标题
 */
fun QMUITopBar.commonTitle(titleGravity: Int? = null, init: (TextView.() -> Unit)? = null) {
    addView(
            TextView(context).apply {
                setPadding(0, 0, autoSize(32), 0)
                id = R.id.action_bar_title
                textSize = 18f
                textColor = Color.WHITE
                gravity = Gravity.CENTER
                typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                maxLines = 1
                maxEms = 15
                ellipsize = TextUtils.TruncateAt.END
                init?.invoke(this)
            },
            RelativeLayout.LayoutParams(wrapContent, matchParent).apply {
                val leftButton = this@commonTitle.findOptional<View>(R.id.qmui_topbar_item_left_back)
                this@commonTitle.findOptional<View>(R.id.top_bar_right_button)?.let {
                    centerHorizontally()
                }
                when {
                    leftButton != null -> {
                        centerHorizontally()
                    }
                    titleGravity == Gravity.START -> alignParentStart()
                    titleGravity == Gravity.END -> alignParentEnd()
                    else -> centerHorizontally()
                }

            }
    )
}

/**
 * 处于中央的标题
 */
fun QMUITopBar.centerTitle(init: (TextView.() -> Unit)? = null) {
    addView(
            TextView(context).apply {
                textSize = 16f
                textColor = Color.WHITE
                gravity = Gravity.CENTER
                init?.invoke(this)
            },
            RelativeLayout.LayoutParams(wrapContent, matchParent).apply {
                centerHorizontally()
            }
    )
}
