/*
 * Copyright (c) 2019. Dylan Cai
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@file:Suppress("unused")

package com.dylanc.loadingstateview

import android.view.View
import androidx.annotation.StringRes
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

typealias viewClickHandler = View.() -> Unit

enum class NavBtnType {
    BACK, ClOSE, DOWN, NONE, INVISIBLE, CUSTOM
}

enum class BgColorType {
    DEFAULT, DISTRICT, SPECIAL_MAP, TRANSPARENT
}

enum class ActivityTransitionType {
    DEFAULT, BOTTOM, ALPHA
}

class ToolbarConfig(
    var title: String? = "",
    var navBtnType: NavBtnType = NavBtnType.BACK,
    val extras: HashMap<String, Any?> = HashMap()
) {
    var colorType: BgColorType = BgColorType.SPECIAL_MAP

    @StringRes
    var showRightIconBtn1: Int? = null
        private set
    var onRightIconBtnBlock1: viewClickHandler? = null
        private set

    @StringRes
    var showRightIconBtn2: Int? = null
        private set
    @StringRes
    var rightIconColorBtn2: Int?=null
        private set
    var onRightIconBtnBlock2: viewClickHandler? = null
        private set

    @StringRes
    var showRightBtn: Int? = null
        private set
    var onRightBtnBlock: viewClickHandler? = null
        private set

    var showConfirmBtn: Boolean? = null
        private set
    var onConfirmBtnBlock: viewClickHandler? = null
        private set

    @StringRes
    var contractUsBtnText: Int? = null
        private set
    var onContractUsBtnBlock: viewClickHandler? = null
        private set

    /**
     * 最右边的iconFont
     */
    fun setRightIconBtn1(@StringRes rightIconBtn: Int?, clickBlock: viewClickHandler? = null) {
        showRightIconBtn1 = rightIconBtn
        onRightIconBtnBlock1 = clickBlock
    }

    /**
     * 第二个iconFont 在第一个左边
     */
    fun setRightIconBtn2(@StringRes rightIconBtn: Int?,@StringRes rightIconColor: Int? = null, clickBlock: viewClickHandler? = null) {
        showRightIconBtn2 = rightIconBtn
        rightIconColorBtn2 = rightIconColor
        onRightIconBtnBlock2 = clickBlock
    }

    /**
     * button组件，在第二个iconfont左边，前面2个不传的话就是在最右边
     */
    fun setRightBtn(@StringRes rightBtn: Int?, clickBlock: viewClickHandler? = null) {
        showRightBtn = rightBtn
        onRightBtnBlock = clickBlock
    }

    /**
     * confirm button 总是最右边
     */
    fun setConfirmBtn(confirmBtn: Boolean = true, clickBlock: viewClickHandler? = null) {
        showConfirmBtn = confirmBtn
        onConfirmBtnBlock = clickBlock
    }

    /**
     * textButton (比如 read all、Contract Us) 总是在最右边
     */
    fun setContractUsBtn(contractUsBtnText: Int?, clickBlock: viewClickHandler? = null) {
        this.contractUsBtnText = contractUsBtnText
        onContractUsBtnBlock = clickBlock
    }
}

fun <T> toolbarExtras() = object : ReadWriteProperty<ToolbarConfig, T?> {
    @Suppress("UNCHECKED_CAST")
    override fun getValue(thisRef: ToolbarConfig, property: KProperty<*>): T? =
        thisRef.extras[property.name] as? T

    override fun setValue(thisRef: ToolbarConfig, property: KProperty<*>, value: T?) {
        thisRef.extras[property.name] = value
    }
}