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.umeng.socialize.media.UMWeb
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 ShareDialog(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 contentLink: String
    private lateinit var title: String
    private val imageBaseUrl = "http://i1.go2yd.com/image.php?url="
    private val imageParams = "&type=thumbnail_200x200"
    private var imageUrl: String? = null
    private lateinit var description: String
    private val dataList: ArrayList<SharePlatformBean> by lazy { ArrayList<SharePlatformBean>() }
    private lateinit var wechat: SharePlatformBean
    private lateinit var wechatMoments: SharePlatformBean
    private lateinit var sinaWeibo: 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(contentLink: String, title: String, description: String, imageUrl: String?){
        this.contentLink = contentLink
        this.title = title
        this.imageUrl = imageUrl
        this.description = description
    }

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

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

    private fun initPlatform(){
        wechat = SharePlatformBean(R.mipmap.icon_share_wx, "微信好友", ShareAdapter.normalType)
        wechatMoments = SharePlatformBean(R.mipmap.icon_share_pyq, "微信朋友圈", ShareAdapter.normalType)
        sinaWeibo = SharePlatformBean(R.mipmap.icon_share_wb, "微博", 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(sinaWeibo)
        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){
        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.sinaBlogPkgName)){
                    shareAction.platform = SHARE_MEDIA.SINA
                }else{
                    Toast.makeText(YdBaseApplication.context, "分享失败，未安装【微博】", Toast.LENGTH_SHORT).show()
                }
            }
            3 -> {
                if(CheckApkExistUtil.checkApkExist(YdBaseApplication.context, CheckApkExistUtil.mobileQQPkgName)){
                    shareAction.platform = SHARE_MEDIA.QQ
                }else{
                    Toast.makeText(YdBaseApplication.context, "分享失败，未安装【QQ】", Toast.LENGTH_SHORT).show()
                }
            }
        }
        val web = UMWeb(contentLink)
        web.title = title
        if(imageUrl != null){
            imageUrl = imageBaseUrl + imageUrl + imageParams
            web.setThumb(UMImage(activity, imageUrl))
        }else{
            web.setThumb(UMImage(activity, R.mipmap.ydlogo))
        }
        web.description = description
        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(web).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
    }
}