/*
 *    Copyright 2018-2019 adview.com, Inc.
 *
 *    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.
 */
package com.adview.mobile.adviewkotlindemo.activities.ads.inapp

import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.adview.mobile.adviewkotlindemo.activities.BaseAdActivity
import com.adview.mobile.adviewkotlindemo.utils.ImageUtils
import com.adview.sdk.AdviewNativeAd
import com.adview.sdk.NativeAdUnit
import com.adview.sdk.rendering.utils.ntv.NativeAdProvider

class InAppNativeActivity : BaseAdActivity() {

    companion object {
        const val CONFIG_ID = "POSIDjdk0claa5fec" //native official
    }

    private var nativeAdUnit: NativeAdUnit? = null

    private var currentTestSizeDp = 320

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        createAd()
    }

    private fun createAd() {
        val extras = Bundle()
        nativeAdUnit = configureNativeAdUnit()

        nativeAdUnit?.fetchDemand(extras) {
            //if extra is null
            if (extras.isEmpty) {
                return@fetchDemand
            }

            inflateAdviewNativeAd2(NativeAdProvider.getNativeAd(extras)!!)
        }
    }

    private fun configureNativeAdUnit(): NativeAdUnit {
        val nativeAdUnit = NativeAdUnit(CONFIG_ID)
        nativeAdUnit.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC)
        nativeAdUnit.setPlacementType(NativeAdUnit.PLACEMENTTYPE.CONTENT_FEED)
        nativeAdUnit.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL)

        return nativeAdUnit
    }

    //test native
    private fun inflateAdviewNativeAd2(ad: AdviewNativeAd) {
        val density = resources.displayMetrics.density
        val sizePx = (currentTestSizeDp * density).toInt()

        val nativeContainer = LinearLayout(this)
        nativeContainer.layoutParams = FrameLayout.LayoutParams(
            sizePx,
            //sizePx
            ViewGroup.LayoutParams.WRAP_CONTENT
        ).apply {
            gravity = Gravity.CENTER
        }
        nativeContainer.orientation = LinearLayout.VERTICAL
        nativeContainer.setBackgroundColor(android.graphics.Color.WHITE) // background
        nativeContainer.setPadding(10, 10, 10, 10)

        // layout in content
        val iconAndTitle = LinearLayout(this)
        iconAndTitle.orientation = LinearLayout.HORIZONTAL
        iconAndTitle.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        ).apply {
            bottomMargin = (10 * density).toInt() // gap
        }

        val icon = ImageView(this)
        val iconSize = (sizePx * 0.15).toInt() // icon smaller
        icon.layoutParams = LinearLayout.LayoutParams(iconSize, iconSize)
        ImageUtils.download(ad.iconUrl, icon)
        iconAndTitle.addView(icon)

        // container
        val textContainer = LinearLayout(this)
        textContainer.orientation = LinearLayout.VERTICAL
        textContainer.layoutParams = LinearLayout.LayoutParams(
            0,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1f
        ).apply {
            leftMargin = (10 * density).toInt() // gap with icon
        }

        val title = TextView(this)
        title.textSize = if (currentTestSizeDp < 150) 12f else 16f //title may larger
        title.typeface = android.graphics.Typeface.DEFAULT_BOLD  //bold
        title.setTextColor(android.graphics.Color.BLACK)
        title.text = ad.title
        textContainer.addView(title)

        val description = TextView(this)
        description.textSize = if (currentTestSizeDp < 150) 8f else 11f // description smaller
        description.maxLines = 2
        description.text = ad.description
        textContainer.addView(description)

        iconAndTitle.addView(textContainer)
        nativeContainer.addView(iconAndTitle)

        val image = ImageView(this)
        // pic fit to size
        val imageWidth = sizePx - 20
        //val imageHeight = (sizePx * 0.4).toInt() // main pic percent
        // 2. height adjust to 0.6
        val imageHeight = (sizePx * 0.6).toInt()
        image.layoutParams = LinearLayout.LayoutParams(imageWidth, imageHeight)
        image.scaleType = ImageView.ScaleType.CENTER_CROP
        ImageUtils.download(ad.imageUrl, image)
        nativeContainer.addView(image)

        if (ad.callToAction != "") {
            val cta = Button(this)
            cta.text = ad.callToAction
            cta.setBackgroundColor(android.graphics.Color.parseColor("#2196F3")) // blue
            cta.setTextColor(android.graphics.Color.WHITE) // white font
            cta.layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                (sizePx * 0.15).toInt() // button percent
            ).apply {
                topMargin = (10 * density).toInt() // white gap
            }
            nativeContainer.addView(cta)
        }

        adWrapperView.addView(nativeContainer)

        ad.registerView(
            nativeContainer, // container
           // listOf(icon, title, image, description, cta),
            listOf(icon, title, image, description),
            null
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        nativeAdUnit?.destroy()
    }

}
