package cn.muchinfo.rma.view.autoWidget

import android.content.Context
import android.graphics.Color
import android.os.Build

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewManager
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.cardview.widget.CardView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.bingoogolapple.bgabanner.BGABanner
import cn.muchinfo.rma.R
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.next.easynavigation.view.EasyNavigationBar
import com.qmuiteam.qmui.alpha.QMUIAlphaButton
import com.qmuiteam.qmui.util.QMUIResHelper
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.*
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
import com.qmuiteam.qmui.widget.tab.QMUITabSegment
import mtp.polymer.com.autowidget.recyclerview.NoAlphaItemAnimator


import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView

inline fun <reified T : View> ViewManager.inflateLayout(layoutRes: Int = 0, init: T.() -> Unit): T {
    return ankoView({ ctx -> LayoutInflater.from(ctx).inflate(layoutRes, null) as T }, theme = 0) { init() }
}

inline fun ViewManager.tabSegment(): QMUITabSegment = tabSegment { }

inline fun ViewManager.tabSegment(init: (@AnkoViewDslMarker QMUITabSegment).() -> Unit): QMUITabSegment {
    return ankoView({ ctx: Context -> QMUITabSegment(ctx) }, theme = 0) { init() }
}

inline fun ViewManager.topBar(): QMUITopBar = topBar { }
inline fun ViewManager.topBar(isTranslucent: Boolean = false, showDivider: Boolean = true, init: (@AnkoViewDslMarker QMUITopBar).() -> Unit): QMUITopBar {
    return ankoView({ ctx: Context -> QMUITopBar(ctx) }, theme = 0) {
        if (showDivider) {
            addView(
                    View(context).apply {
                        background = resources.getDrawable(R.color.main_title_bg_color)
                    },
                    RelativeLayout.LayoutParams(matchParent, autoSize(1)).apply {
                        alignParentBottom()
                    }
            )
        }
        val statusBarHeight = QMUIStatusBarHelper.getStatusbarHeight(context)
        val value = QMUIResHelper.getAttrDimen(context, R.attr.qmui_topbar_height)
        var topBarHeight = autoSize(value)
        if (isTranslucent) {
            topBarHeight += statusBarHeight
            setPadding(0, statusBarHeight, 0, 0)
        } else {
            padding = 0
        }
        layoutParams = ViewGroup.MarginLayoutParams(matchParent, autoSize(120))
        background = resources.getDrawable(R.color.main_title_bg_color)
        init()
    }
}


inline fun ViewManager.topBarTwo(): QMUITopBar = topBarTwo { }
inline fun ViewManager.topBarTwo(isTranslucent: Boolean = false, showDivider: Boolean = true, init: (@AnkoViewDslMarker QMUITopBar).() -> Unit): QMUITopBar {
    return ankoView({ ctx: Context -> QMUITopBar(ctx) }, theme = 0) {
        if (showDivider) {
            addView(
                    View(context).apply {
                        background = resources.getDrawable(R.color.page_background_bg)
                    },
                    RelativeLayout.LayoutParams(matchParent, autoSize(1)).apply {
                        alignParentBottom()
                    }
            )
        }
        val statusBarHeight = QMUIStatusBarHelper.getStatusbarHeight(context)
        val value = QMUIResHelper.getAttrDimen(context, R.attr.qmui_topbar_height)
        var topBarHeight = autoSize(value)
        if (isTranslucent) {
            topBarHeight += statusBarHeight
            setPadding(0, statusBarHeight, 0, 0)
        } else {
            padding = 0
        }
        layoutParams = ViewGroup.MarginLayoutParams(matchParent, autoSize(40))
        background = resources.getDrawable(R.color.rma_title_bg)
        init()
    }
}


inline fun ViewManager.commonButton(init: (@AnkoViewDslMarker QMUIAlphaButton).() -> Unit): QMUIAlphaButton {
    return ankoView({ ctx: Context ->
        QMUIAlphaButton(ctx).apply {
            padding = 0
            textColor = Color.WHITE
            textSizeAuto = 40
            backgroundDrawable = createRoundRectDrawable("#159dfd", 24)
        }
    }, theme = 0) { init() }
}

inline fun ViewManager.roundButton(init: (@AnkoViewDslMarker QMUIRoundButton).() -> Unit): QMUIRoundButton {
    return ankoView({ ctx: Context -> QMUIRoundButton(ctx).apply { padding = 0 } }, theme = 0) { init() }
}

inline fun ViewManager.banner(): BGABanner = banner { }
inline fun ViewManager.banner(init: (@AnkoViewDslMarker BGABanner).() -> Unit): BGABanner {
    return ankoView({ ctx: Context -> BGABanner(ctx, null) }, theme = 0) { init() }
}


inline fun ViewManager.interceptScrollContainer() : InterceptScrollContainer = interceptScrollContainer{}
inline fun ViewManager.interceptScrollContainer(init: (@AnkoViewDslMarker InterceptScrollContainer).() -> Unit) :InterceptScrollContainer{
    return ankoView({ ctx: Context -> InterceptScrollContainer(ctx) }, theme = 0) { init() }
}

inline fun ViewManager.syncHScrollView() : SyncHScrollView = syncHScrollView{}
inline fun ViewManager.syncHScrollView(init: (@AnkoViewDslMarker SyncHScrollView).() -> Unit) :SyncHScrollView{
    return ankoView({ ctx: Context -> SyncHScrollView(ctx) }, theme = 0) { init() }
}

//inline fun ViewManager.banner(): Banner = banner { }
//inline fun ViewManager.banner(init: (@AnkoViewDslMarker Banner).() -> Unit): Banner {
//    return ankoView({ ctx: Context -> Banner(ctx, null) }, theme = 0) { init() }
//}

//inline fun ViewManager.hBanner(): HBanner = hBanner { }
//inline fun ViewManager.hBanner(init: (@AnkoViewDslMarker HBanner).() -> Unit): HBanner {
//    return ankoView({ ctx: Context -> HBanner(ctx, null) }, theme = 0) { init() }
//}

inline fun ViewManager.coordinatorLayout(): CoordinatorLayout = coordinatorLayout { }
inline fun ViewManager.coordinatorLayout(init: (@AnkoViewDslMarker CoordinatorLayout).() -> Unit): CoordinatorLayout {
    return ankoView({ ctx: Context -> CoordinatorLayout(ctx, null) }, theme = 0) { init() }
}

//inline fun ViewManager.flowLayout() : TagFlowLayout = flowLayout{}
//inline fun ViewManager.flowLayout(init:(@AnkoViewDslMarker TagFlowLayout).() -> Unit) : TagFlowLayout{
//    return ankoView({ctx: Context -> TagFlowLayout(ctx,null) },theme = 0) { init() }
//}


//inline fun ViewManager.lineChart(): LineChart = lineChart { }
//inline fun ViewManager.lineChart(init: (@AnkoViewDslMarker LineChart).() -> Unit): LineChart {
//    return ankoView({ ctx: Context -> LineChart(ctx, null) }, theme = 0) { init() }
//}


//inline fun ViewManager.mylineChart(): MyLineChart = mylineChart { }
//inline fun ViewManager.mylineChart(init: (@AnkoViewDslMarker MyLineChart).() -> Unit): MyLineChart {
//    return ankoView({ ctx: Context -> MyLineChart(ctx, null) }, theme = 0) { init() }
//}

//inline fun ViewManager.myLineChart():MyLineChart = myLineChart { }
//inline fun ViewManager.myLineChart(init:(@AnkoViewDslMarker MyLineChart).() -> Unit) : MyLineChart{
//    return ankoView({ctx: Context -> MyLineChart(ctx,null) },theme = 0){ init() }
//}

//inline fun ViewManager.captCha(): Captcha = captCha { }
//inline fun ViewManager.captCha(init: (@AnkoViewDslMarker Captcha).() -> Unit): Captcha {
//    return ankoView({ ctx: Context -> Captcha(ctx) }, theme = 0) { init() }
//}


inline fun ViewManager.nestedScrollview(): NestedScrollView = nestedScrollview { }
inline fun ViewManager.nestedScrollview(init: (@AnkoViewDslMarker NestedScrollView).() -> Unit): NestedScrollView {
    return ankoView({ ctx: Context -> NestedScrollView(ctx) }, theme = 0) { init() }
}


//inline fun ViewManager.moneyEditText(): MoneyEditText = moneyEditText {}
//inline fun ViewManager.moneyEditText(init: (@AnkoViewDslMarker MoneyEditText).() -> Unit): MoneyEditText {
//    return ankoView({ ctx: Context -> MoneyEditText(ctx) }, theme = 0) { init() }
//}

//inline fun ViewManager.linePathView(): LinePathView = linePathView {}
//inline fun ViewManager.linePathView(init: (@AnkoViewDslMarker LinePathView).() -> Unit): LinePathView {
//    return ankoView({ ctx: Context -> LinePathView(ctx) }, theme = 0) { init() }
//}

inline fun ViewManager.appBarLayout(): QMUIAppBarLayout = appBarLayout { }
inline fun ViewManager.appBarLayout(init: (@AnkoViewDslMarker QMUIAppBarLayout).() -> Unit): QMUIAppBarLayout {
    return ankoView({ ctx: Context -> QMUIAppBarLayout(ctx) }, theme = 0) { init() }
}

inline fun ViewManager.topBarLayout(): QMUICollapsingTopBarLayout = topBarLayout { }
inline fun ViewManager.topBarLayout(init: (@AnkoViewDslMarker QMUICollapsingTopBarLayout).() -> Unit): QMUICollapsingTopBarLayout {
    return ankoView({ ctx: Context -> QMUICollapsingTopBarLayout(ctx) }, theme = 0) { init() }
}

//inline fun ViewManager.autoSizeText(): AutoSizeTextView = autoSizeText { }
//inline fun ViewManager.autoSizeText(init: (@AnkoViewDslMarker AutoSizeTextView).() -> Unit): AutoSizeTextView {
//    return ankoView({ ctx: Context -> AutoSizeTextView(ctx, null) }, theme = 0) { init() }
//}

//inline fun ViewManager.numberEditText():MangeNumberEditText2 = numberEditText{}
//inline fun ViewManager.numberEditText(init: (@AnkoViewDslMarker MangeNumberEditText2).() -> Unit) : MangeNumberEditText2{
//    return ankoView({ctx: Context -> MangeNumberEditText2(ctx,null) },theme = 0){ init() }
//}

//inline fun ViewManager.mangeNumberEditText2(): MangeNumberEditText2 = mangeNumberEditText2 { }
//inline fun ViewManager.mangeNumberEditText2(init: (@AnkoViewDslMarker MangeNumberEditText2).() -> Unit): MangeNumberEditText2 {
//    return ankoView({ ctx: Context -> MangeNumberEditText2(ctx, null) }, theme = 0) { init() }
//}

inline fun ViewManager.groupList(): QMUIGroupListView = groupList {}
inline fun ViewManager.groupList(init: (@AnkoViewDslMarker QMUIGroupListView).() -> Unit): QMUIGroupListView {
    return ankoView({ ctx: Context -> QMUIGroupListView(ctx) }, theme = 0) { init() }
}

fun QMUIGroupListView.newSection(block: (QMUIGroupListView.Section.() -> Unit)) {
    val newSection = QMUIGroupListView.newSection(context)
    block.invoke(newSection)
    newSection.addTo(this)
}

inline fun ViewManager.textInputLayout(): TextInputLayout = textInputLayout {}
inline fun ViewManager.textInputLayout(init: (@AnkoViewDslMarker TextInputLayout).() -> Unit): TextInputLayout {
    return ankoView({ ctx: Context -> TextInputLayout(ctx) }, theme = 0) { init() }
}

//inline fun ViewManager.autoTextView(): AutoSizeTextView = autoTextView {}
//inline fun ViewManager.autoTextView(init: (@AnkoViewDslMarker AutoSizeTextView).() -> Unit): AutoSizeTextView {
//    return ankoView({ ctx: Context -> AutoSizeTextView(ctx, null) }, theme = 0) { init() }
//}

inline fun ViewManager.textInputEditText(): TextInputEditText = textInputEditText {}
inline fun ViewManager.textInputEditText(init: (@AnkoViewDslMarker TextInputEditText).() -> Unit): TextInputEditText {
    return ankoView({ ctx: Context -> TextInputEditText(ctx, null) }, theme = 0) { init() }
}

inline fun ViewManager.cardView(): CardView = cardView {}
inline fun ViewManager.cardView(init: (@AnkoViewDslMarker CardView).() -> Unit): CardView {
    return ankoView({ ctx: Context -> CardView(ctx).apply { backgroundColor = Color.TRANSPARENT } }, theme = 0) { init() }
}

//inline fun ViewManager.headView(): HeadView = headView {}
//inline fun ViewManager.headView(init: (@AnkoViewDslMarker HeadView).() -> Unit): HeadView {
//    return ankoView({ ctx: Context -> HeadView(ctx, null) }, theme = 0) { init() }
//}

inline fun ViewManager.swipeToLoadLayout(): SwipeToLoadLayout = swipeToLoadLayout {}
inline fun ViewManager.swipeToLoadLayout(init: (@AnkoViewDslMarker SwipeToLoadLayout).() -> Unit): SwipeToLoadLayout {
    return ankoView({ ctx: Context -> SwipeToLoadLayout(ctx) }, theme = 0) { init() }
}

inline fun ViewManager.nestedScrollView():NestedScrollView  = nestedScrollView {}
inline fun ViewManager.nestedScrollView(init: (@AnkoViewDslMarker NestedScrollView).() -> Unit): NestedScrollView {
    return ankoView({ ctx: Context -> NestedScrollView(ctx) }, theme = 0) { init() }
}

inline fun ViewManager.easyNavigationBar(): EasyNavigationBar = easyNavigationBar {}
inline fun ViewManager.easyNavigationBar(init: (@AnkoViewDslMarker EasyNavigationBar).() -> Unit): EasyNavigationBar {
    return ankoView({ ctx: Context -> EasyNavigationBar(ctx) }, theme = 0) { init() }
}

//inline fun ViewManager.gridLayoutV7(): GridLayoutV7 = gridLayoutV7 {}
//inline fun ViewManager.gridLayoutV7(init: (@AnkoViewDslMarker GridLayoutV7).() -> Unit): GridLayoutV7 {
//    return ankoView({ ctx: Context -> GridLayoutV7(ctx) }, theme = 0) { init() }
//}



inline fun ViewManager.recyclerView(init: (@AnkoViewDslMarker RecyclerView).() -> Unit): RecyclerView {
    return ankoView({ ctx: Context -> RecyclerView(ctx) }, theme = 0) {
        layoutManager = LinearLayoutManager(context)
        itemAnimator = NoAlphaItemAnimator()
        init()
    }
}

fun ViewManager.loadingView(): QMUILoadingView = loadingView {}
fun ViewManager.loadingView(init: (@AnkoViewDslMarker QMUILoadingView).() -> Unit): QMUILoadingView {
    return ankoView({ QMUILoadingView(it) }, theme = 0) { init() }
}
//
//fun ViewManager.sideLetterBar(): SideLetterBar = sideLetterBar {}
//fun ViewManager.sideLetterBar(init: (@AnkoViewDslMarker SideLetterBar).() -> Unit): SideLetterBar {
//    return ankoView({ SideLetterBar(it) }, theme = 0) { init() }
//}

fun LinearLayout.space(autoSize: Int) {
    space().apply {
        layoutParams = if (orientation == LinearLayout.VERTICAL) {
            LinearLayout.LayoutParams(matchParent, autoSize(autoSize))
        } else {
            LinearLayout.LayoutParams(autoSize(autoSize), matchParent)
        }
    }
}

fun LinearLayout.divider(bgColor: String = "#323232", size: Int = 1, paddingStart: Int = 30, paddingEnd: Int = paddingStart): View {
    return view().apply {
        backgroundColor = Color.parseColor(bgColor)
        layoutParams = if (orientation == LinearLayout.VERTICAL) {
            LinearLayout.LayoutParams(matchParent, autoSize(size)).apply {
                marginStart = autoSize(paddingStart)
                marginEnd = autoSize(paddingEnd)
            }
        } else {
            LinearLayout.LayoutParams(autoSize(size), matchParent).apply {
                topMargin = autoSize(paddingStart)
                bottomMargin = autoSize(paddingEnd)
            }
        }
    }
}

fun ViewManager.statusBarInTranslucent() {
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
        ankoView({ ctx: Context ->
            View(ctx).apply {
                backgroundColor = QMUIResHelper.getAttrColor(context, R.attr.colorPrimaryDark)
                layoutParams = ViewGroup.LayoutParams(matchParent, QMUIStatusBarHelper.getStatusbarHeight(context))
            }
        }, theme = 0) { }
    }
}
//
//inline fun ViewManager.lottie(): LottieAnimationView = lottie { }
//inline fun ViewManager.lottie(init: (@AnkoViewDslMarker LottieAnimationView).() -> Unit): LottieAnimationView {
//    return ankoView({ ctx: Context -> LottieAnimationView(ctx, null) }, theme = 0) { init() }
//}
