package kr.co.vividnext.sodalive.audio_content.series.detail

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import com.google.android.material.shape.ShapeAppearanceModel
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.base.BaseFragment
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.databinding.FragmentSeriesDetailIntroductionBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class SeriesDetailIntroductionFragment : BaseFragment<FragmentSeriesDetailIntroductionBinding>(
    FragmentSeriesDetailIntroductionBinding::inflate
) {
    private var seriesDetailResponse: GetSeriesDetailResponse? = null

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

        if (arguments != null) {
            seriesDetailResponse = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                requireArguments().getParcelable(
                    Constants.EXTRA_SERIES,
                    GetSeriesDetailResponse::class.java
                )
            } else {
                requireArguments().getParcelable(Constants.EXTRA_SERIES)
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (seriesDetailResponse != null) {
            setSeriesKeywordChipList(seriesDetailResponse!!.keywordList)
            setSeriesIntroduction(seriesDetailResponse!!.introduction)
            setSeriesPrice()
            setSeriesInfo()
        }
    }

    private fun setSeriesPrice() {
        val rentalMinPrice = seriesDetailResponse!!.rentalMinPrice
        val rentalMaxPrice = seriesDetailResponse!!.rentalMaxPrice
        val minPrice = seriesDetailResponse!!.minPrice
        val maxPrice = seriesDetailResponse!!.maxPrice

        binding.tvRentalPrice.text = if (rentalMinPrice == rentalMaxPrice) {
            if (rentalMaxPrice == 0) {
                "무료(15일)"
            } else {
                "$rentalMaxPrice(15일)"
            }
        } else {
            "${if (rentalMinPrice == 0) "무료" else rentalMinPrice} ~ ${rentalMaxPrice}캔 (15일)"
        }

        binding.tvPrice.text = if (minPrice == maxPrice) {
            if (maxPrice == 0) {
                "무료"
            } else {
                "$maxPrice"
            }
        } else {
            "${if (minPrice == 0) "무료" else minPrice} ~ ${maxPrice}캔"
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setSeriesInfo() {
        binding.tvGenre.text = seriesDetailResponse!!.genre
        binding.tvIsAdult.text = if (seriesDetailResponse!!.isAdult) {
            "19세 이상"
        } else {
            "전체연령가"
        }

        binding.tvPublishedDate.text = seriesDetailResponse!!.publishedDate
        binding.tvPublishedDaysOfWeek.text =
            if (seriesDetailResponse!!.publishedDaysOfWeek == "랜덤") {
                seriesDetailResponse!!.publishedDaysOfWeek
            } else {
                seriesDetailResponse!!.publishedDaysOfWeek
            }

        if (seriesDetailResponse!!.writer != null) {
            binding.tvWriter.visibility = View.VISIBLE
            binding.tvWriterLabel.visibility = View.VISIBLE

            binding.tvWriter.text = seriesDetailResponse!!.writer
        } else {
            binding.tvWriter.visibility = View.GONE
            binding.tvWriterLabel.visibility = View.GONE
        }

        if (seriesDetailResponse!!.studio != null) {
            binding.tvStudio.visibility = View.VISIBLE
            binding.tvStudioLabel.visibility = View.VISIBLE

            binding.tvStudio.text = seriesDetailResponse!!.studio
        } else {
            binding.tvStudio.visibility = View.GONE
            binding.tvStudioLabel.visibility = View.GONE
        }
    }

    private fun setSeriesIntroduction(introduction: String) {
        binding.tvIntroduce.text = introduction
    }

    private fun setSeriesKeywordChipList(keywordList: List<String>) {
        binding.chipGroup.isHorizontalScrollBarEnabled = false

        for (keyword in keywordList) {
            val chip = Chip(requireActivity())
            chip.text = keyword
            chip.isClickable = false
            chip.isCheckable = false
            chip.textSize = 12f
            chip.chipStrokeWidth = 0f
            chip.setChipBackgroundColorResource(R.color.color_222222)

            val shapeAppearanceModel = ShapeAppearanceModel.Builder()
                .setAllCornerSizes(26.7f.dpToPx())
                .build()
            chip.shapeAppearanceModel = shapeAppearanceModel

            chip.setEnsureMinTouchTargetSize(false)
            chip.setPadding(0, 0, 0, 0)
            chip.setTextColor(ContextCompat.getColor(requireContext(), R.color.color_d2d2d2))
            chip.typeface = ResourcesCompat.getFont(requireContext(), R.font.gmarket_sans_medium)
            binding.chipGroup.addView(chip)
        }
    }
}
