package com.swago.room.dialog

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.fragment.app.viewModels
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.SwagoShareUtils
import com.swago.room.R
import com.swago.room.databinding.DialogShareBinding
import com.swago.room.vm.RoomOtherVm

/**
 *@date 2022/3/29 21:00
 *description:
 */
class ShareDialog : BaseXDFragment<DialogShareBinding>() {

    private val roomOtherVm by viewModels<RoomOtherVm>()
    private var shareUrl = ""

    val clipBoard by lazy {
        requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    }

    init {
        setGravity(Gravity.BOTTOM)
        setCanCancel(true)
        setDimAmount(0f)
    }

    companion object{
        fun newInstance(): ShareDialog {
            val args = Bundle()
            val fragment = ShareDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        roomOtherVm.getShareConfig()
        binding.tvCopy.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                val clip = ClipData.newPlainText("share", shareUrl)
                clipBoard.setPrimaryClip(clip)
                Toast.makeText(AppContext.getContext(), "Copy success", Toast.LENGTH_SHORT).show()
            }
        })

        binding.tvFacebook.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                activity?.let {
                    if (SwagoShareUtils.isInstall(it,SwagoShareUtils.FACEBOOK)){
                        SwagoShareUtils.shareToFacebook(shareUrl,it)
                    }else{
                        Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(
                            R.string.app_not_install), Toast.LENGTH_SHORT).show()
                    }
                }
            }
        })

        binding.tvLine.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                activity?.let {
                    if (SwagoShareUtils.isInstall(it,SwagoShareUtils.LINE)){
                        SwagoShareUtils.shareToLINE(shareUrl,it)
                    }else{
                        Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(
                            R.string.app_not_install), Toast.LENGTH_SHORT).show()
                    }
                }
            }
        })

        binding.tvWhatsApp.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                activity?.let {
                    if (SwagoShareUtils.isInstall(it,SwagoShareUtils.WHATSAPP)){
                        SwagoShareUtils.shareToWhatsApp(shareUrl,it)
                    }else{
                        Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(
                            R.string.app_not_install), Toast.LENGTH_SHORT).show()
                    }
                }
            }
        })
    }

    override fun initLiveData() {
        roomOtherVm.shareConfigLiveData.observe(this){
            shareUrl = it.share_link
        }
    }

}