package com.yidian.shenghuoquan.newscontent.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.yidian.common.YdBaseApplication
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.common.utils.CheckApkExistUtil
import com.yidian.nightmode.widget.YdRecyclerView
import com.yidian.nightmode.widget.YdTextView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.ShareAdapter
import com.yidian.shenghuoquan.newscontent.bean.SharePlatformBean

class ShareImageDialog(private val activity: Activity, themeResId: Int): Dialog(activity, themeResId) {
    private lateinit var rvSharePlatform: YdRecyclerView
    private lateinit var tvCancel: YdTextView
    private lateinit var shareAdapter: ShareAdapter
    private lateinit var imageUrl: String
    private val dataList: ArrayList<SharePlatformBean> by lazy { ArrayList<SharePlatformBean>() }
    private lateinit var wechat: SharePlatformBean
    private lateinit var wechatMoments: SharePlatformBean
    private lateinit var qq: SharePlatformBean
    private lateinit var copyLink: SharePlatformBean

    init {
        setDialog()
    }

    private fun setDialog(){
        this.setCanceledOnTouchOutside(true)
        this.setContentView(R.layout.share_dialog)
        this.setOwnerActivity(activity)
        window?.setWindowAnimations(R.style.PopupWindow_anim_style)
        rvSharePlatform = findViewById(R.id.rv_sharePlatform)
        tvCancel = findViewById(R.id.tv_cancel)
        shareAdapter = ShareAdapter()
        initPlatform()
        setClickListener()
    }

    fun setData(imageUrl: String){
        this.imageUrl = imageUrl
    }

    private fun setClickListener(){
        tvCancel.setOnClickListener{
            dismiss()
        }

        shareAdapter.setOnItemClickListener(object : OnItemClickListener<SharePlatformBean> {
            override fun onClick(view: View?, position: Int, item: SharePlatformBean?) {
                when (position) {
                    in 0..2 -> oneKeyShare(position, imageUrl)
                    3 -> copyLink(imageUrl)
                }
            }
        })
    }

    private fun initPlatform(){
        wechat = SharePlatformBean(R.mipmap.icon_share_wx, "微信好友", ShareAdapter.normalType)
        wechatMoments = SharePlatformBean(R.mipmap.icon_share_pyq, "微信朋友圈", ShareAdapter.normalType)
        qq = SharePlatformBean(R.mipmap.icon_share_qq, "手机QQ", ShareAdapter.normalType)
        copyLink = SharePlatformBean(R.mipmap.icon_share_link, "复制链接", ShareAdapter.footType)
        dataList.add(wechat)
        dataList.add(wechatMoments)
        dataList.add(qq)
        dataList.add(copyLink)
        val layoutManager = LinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        rvSharePlatform.layoutManager = layoutManager
        rvSharePlatform.adapter = shareAdapter
        shareAdapter.update(dataList)
    }

    private fun copyLink(text: String){
        val cmb = activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
        cmb?.setPrimaryClip(ClipData.newPlainText(null, text))
        Toast.makeText(activity, "复制成功", Toast.LENGTH_SHORT).show()
        dismiss()
    }

    private fun oneKeyShare(position: Int, imageUrl: String){
        val shareAction = ShareAction(activity)
        when(position){
            0 -> {
                if(CheckApkExistUtil.checkApkExist(YdBaseApplication.context, CheckApkExistUtil.weChatPkgName)){
                    shareAction.platform = SHARE_MEDIA.WEIXIN
                }else{
                    Toast.makeText(YdBaseApplication.context, "分享失败，未安装【微信】", Toast.LENGTH_SHORT).show()
                }
            }
            1 -> {
                if(CheckApkExistUtil.checkApkExist(YdBaseApplication.context, CheckApkExistUtil.weChatPkgName)){
                    shareAction.platform = SHARE_MEDIA.WEIXIN_CIRCLE
                }else{
                    Toast.makeText(YdBaseApplication.context, "分享失败，未安装【微信】", Toast.LENGTH_SHORT).show()
                }
            }
            2 -> {
                if(CheckApkExistUtil.checkApkExist(YdBaseApplication.context, CheckApkExistUtil.mobileQQPkgName)){
                    shareAction.platform = SHARE_MEDIA.QQ
                }else{
                    Toast.makeText(YdBaseApplication.context, "分享失败，未安装【QQ】", Toast.LENGTH_SHORT).show()
                }
            }
        }
        val image = UMImage(activity, imageUrl)
        val thumb = UMImage(activity, imageUrl)
        image.setThumb(thumb)
        shareAction.setCallback(object : UMShareListener {
            override fun onStart(platform: SHARE_MEDIA?) {

            }

            override fun onResult(platform: SHARE_MEDIA?) {
                Toast.makeText(activity, "分享成功", Toast.LENGTH_SHORT).show()
            }

            override fun onError(platform: SHARE_MEDIA?, t: Throwable?) {
                Log.e("zhb-yd", "失败" + t?.message)
            }

            override fun onCancel(platform: SHARE_MEDIA?) {
                Toast.makeText(activity, "分享取消", Toast.LENGTH_SHORT).show()
            }

        }).withMedia(image).share()
        dismiss()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val params: WindowManager.LayoutParams? = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.attributes?.gravity = Gravity.BOTTOM
    }
}