package kr.co.vividnext.sodalive.settings.event

import android.content.Intent
import android.net.Uri
import android.view.View
import android.widget.FrameLayout
import android.widget.RelativeLayout
import androidx.core.content.IntentCompat
import coil.load
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.databinding.ActivityEventDetailBinding

class EventDetailActivity : BaseActivity<ActivityEventDetailBinding>(
    ActivityEventDetailBinding::inflate
) {
    private lateinit var event: EventItem

    override fun setupView() {
        event = IntentCompat.getParcelableExtra(
            intent,
            Constants.EXTRA_EVENT,
            EventItem::class.java
        )!!

        binding.toolbar.tvBack.text = "이벤트 상세"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        if (!event.link.isNullOrBlank()) {
            binding.flParticipate.visibility = View.VISIBLE

            val flParticipateLp =
                binding.flParticipate.layoutParams as RelativeLayout.LayoutParams

            val ivEventLp = binding.ivEvent.layoutParams as FrameLayout.LayoutParams
            ivEventLp.bottomMargin = flParticipateLp.height
            binding.ivEvent.layoutParams = ivEventLp
        } else {
            binding.flParticipate.visibility = View.GONE
        }

        binding.tvParticipate.setOnClickListener {
            if (!event.link.isNullOrBlank()) {
                startActivity(
                    Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(event.link)
                    )
                )
            }
        }

        binding.ivEvent.load(event.detailImageUrl)
    }
}
