package com.kdanmobile.kdan_others_library_for_android.card

import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.bumptech.glide.Glide
import com.kdanmobile.kdan_others_library_for_android.R
import com.kdanmobile.kdan_others_library_for_android.model.CreativeStoreRemoteData
import com.kdanmobile.kdan_others_library_for_android.view.SimpleWebViewActivity


class CreativeStoreRemoteCard : BaseCard {

    constructor(context: Context) : super(context)

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)

    var listener: Listener? = null
    var defaultMediaImgRes: Int = R.mipmap.cd_creative_store

    fun setPrimaryTextBackgroundRes(resId: Int) {
        primaryTextTextView.setBackgroundResource(resId)
    }

    fun setPrimaryTextColor(color: Int) {
        primaryTextTextView.setTextColor(color)
    }

    fun setButtonTextColor(color: Int) {
        action1Button.setTextColor(color)
        action2Button.setTextColor(color)
        action3Button.setTextColor(color)
    }

    fun setRemoteData(creativeStoreRemoteData: CreativeStoreRemoteData) {
        creativeStoreRemoteData.getTitle()?.let {
            primaryTextTextView.visibility = View.VISIBLE
            primaryTextTextView.text = it
        }
        creativeStoreRemoteData.getContent()?.let {
            supportingTextTextView.visibility = View.VISIBLE
            supportingTextTextView.text = it
        }
        creativeStoreRemoteData.getImgUrl()?.let {
            Glide.with(context)
                    .load(it)
                    .placeholder(defaultMediaImgRes)
                    .into(mediaImageView)
        }
        creativeStoreRemoteData.getImgClickUrl()?.apply {
            mediaImageView.setOnClickListener {
                if (listener == null) {
                    SimpleWebViewActivity.launch(context, this)
                } else {
                    listener?.onClickLinkView(it, this)
                }
            }
        }
        for (btnText in creativeStoreRemoteData.getBtnTextList()) {
            btnText?.let {
                when (creativeStoreRemoteData.getBtnTextList().indexOf(it)) {
                    0 -> {
                        action1Button.visibility = View.VISIBLE
                        action1Button.text = it
                    }

                    1 -> {
                        action2Button.visibility = View.VISIBLE
                        action2Button.text = it
                    }

                    2 -> {
                        action3Button.visibility = View.VISIBLE
                        action3Button.text = it
                    }
                }
            }
        }
        for (btnUrl in creativeStoreRemoteData.getBtnUrlList()) {
            btnUrl?.let {
                when (creativeStoreRemoteData.getBtnUrlList().indexOf(btnUrl)) {
                    0 -> {
                        action1Button.setOnClickListener {
                            if (listener == null) {
                                SimpleWebViewActivity.launch(context, btnUrl)
                            } else {
                                listener?.onClickLinkView(it, btnUrl)
                            }
                        }
                    }
                    1 -> {
                        action2Button.setOnClickListener {
                            if (listener == null) {
                                SimpleWebViewActivity.launch(context, btnUrl)
                            } else {
                                listener?.onClickLinkView(it, btnUrl)
                            }
                        }
                    }
                    2 -> {
                        action3Button.setOnClickListener {
                            if (listener == null) {
                                SimpleWebViewActivity.launch(context, btnUrl)
                            } else {
                                listener?.onClickLinkView(it, btnUrl)
                            }
                        }
                    }
                }
            }
        }
    }

    interface Listener {
        fun onClickLinkView(view: View, url: String)
    }
}