package kr.co.vividnext.sodalive.message.text

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.core.content.IntentCompat
import coil.load
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ActivityTextMessageDetailBinding
import kr.co.vividnext.sodalive.extensions.convertDateFormat
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.message.GetTextMessageResponse
import kr.co.vividnext.sodalive.message.MessageBox
import org.koin.android.ext.android.inject

class TextMessageDetailActivity : BaseActivity<ActivityTextMessageDetailBinding>(
    ActivityTextMessageDetailBinding::inflate
) {
    private val viewModel: TextMessageDetailViewModel by inject()

    private var messageItem: GetTextMessageResponse.TextMessageItem? = null

    private lateinit var loadingDialog: LoadingDialog

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

    override fun setupView() {
        loadingDialog = LoadingDialog(this, layoutInflater)

        messageItem = IntentCompat.getParcelableExtra(
            intent,
            Constants.EXTRA_TEXT_MESSAGE,
            GetTextMessageResponse.TextMessageItem::class.java
        )

        val messageBox = intent.getStringExtra(Constants.EXTRA_MESSAGE_BOX)

        if (messageItem == null || messageBox == null) {
            Toast.makeText(applicationContext, R.string.retry, Toast.LENGTH_LONG).show()
            finish()
        }

        viewModel.messageId = messageItem!!.messageId

        when {
            messageBox!! == MessageBox.RECEIVE.name -> {
                binding.toolbar.tvBack.text = "받은 메시지 상세"
                binding.tvKeep.visibility = View.VISIBLE
                binding.tvReply.visibility = View.VISIBLE
                binding.tvDelete.visibility = View.VISIBLE
            }

            messageBox == MessageBox.KEEP.name -> {
                binding.toolbar.tvBack.text = "저장한 메시지 상세"
                binding.tvKeep.visibility = View.GONE
                binding.tvReply.visibility = View.GONE
                binding.tvDelete.visibility = View.VISIBLE
            }

            else -> {
                binding.toolbar.tvBack.text = "보낸 메시지 상세"
                binding.tvKeep.visibility = View.GONE
                binding.tvReply.visibility = View.GONE
                binding.tvDelete.visibility = View.VISIBLE
            }
        }

        binding.toolbar.tvBack.setOnClickListener { finish() }
        binding.tvReply.setOnClickListener {
            val intent = Intent(applicationContext, TextMessageWriteActivity::class.java)
            intent.putExtra(Constants.EXTRA_USER_ID, messageItem!!.senderId)
            intent.putExtra(Constants.EXTRA_NICKNAME, messageItem!!.senderNickname)
            startActivity(intent)
        }

        binding.tvKeep.setOnClickListener {
            if (messageItem!!.isKept) {
                Toast.makeText(
                    applicationContext,
                    "이미 보관된 메시지 입니다.",
                    Toast.LENGTH_LONG
                ).show()
                return@setOnClickListener
            }

            viewModel.keepTextMessage()
        }
        binding.tvDelete.setOnClickListener {
            viewModel.deleteMessage {
                Toast.makeText(
                    applicationContext,
                    "메시지가 삭제되었습니다.",
                    Toast.LENGTH_LONG
                ).show()

                setResult(RESULT_OK)
                finish()
            }
        }

        setData()
    }

    private fun setData() {
        if (SharedPreferenceManager.nickname == messageItem?.recipientNickname) {
            binding.ivProfile.load(messageItem?.senderProfileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(13.3f.dpToPx()))
            }

            binding.tvNickname.text = messageItem?.senderNickname
        } else {
            binding.ivProfile.load(messageItem?.recipientProfileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(13.3f.dpToPx()))
            }

            binding.tvNickname.text = messageItem?.recipientNickname
        }

        binding.tvMessage.text = messageItem?.textMessage
        binding.tvDate.text = messageItem?.date!!.convertDateFormat(
            from = "yyyy-MM-dd hh:mm:ss",
            to = "yyyy년 MM월 dd일 E요일 HH:mm"
        )
    }

    private fun bindData() {
        viewModel.toastLiveData.observe(this) {
            it?.let { Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show() }
        }

        viewModel.isLoading.observe(this) {
            if (it) {
                loadingDialog.show(screenWidth)
            } else {
                loadingDialog.dismiss()
            }
        }
    }
}
