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

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
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.common.utils.YdFileUtils
import com.yidian.nightmode.widget.YdRecyclerView
import com.yidian.nightmode.widget.YdRelativeLayout
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 SharePosterDialog(private val activity: Activity, themeResId: Int): Dialog(activity, themeResId) {
    private lateinit var rvSharePlatform: YdRecyclerView
    private lateinit var tvCancel: YdTextView
    private lateinit var rlyRoot: YdRelativeLayout
    private lateinit var rlyPoster: YdRelativeLayout
    private lateinit var shareAdapter: ShareAdapter
    private lateinit var contentLink: String
    private lateinit var poster: View
    private val dataList: ArrayList<SharePlatformBean> by lazy { ArrayList<SharePlatformBean>() }
    private lateinit var saveImage: SharePlatformBean
    private lateinit var wechat: SharePlatformBean
    private lateinit var wechatMoments: SharePlatformBean
    private lateinit var qq: SharePlatformBean

    init {
        setDialog()
    }

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

    fun setData(contentLink: String, poster: View, ivQRCode: ImageView){
        this.contentLink = contentLink
        this.poster = poster
        initPoster(ivQRCode)
    }

    private fun initPoster(ivQRCode: ImageView){
        val qrCodeBitmap = YdFileUtils.createQRCodeBitmap(contentLink, 200, 200)
        if(qrCodeBitmap != null){
            ivQRCode.setImageBitmap(qrCodeBitmap)
        }
        rlyPoster.addView(poster)
    }

    private fun initPlatform(){
        saveImage = SharePlatformBean(R.mipmap.icon_save_card, "保存图片", ShareAdapter.headType)
        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)
        dataList.add(saveImage)
        dataList.add(wechat)
        dataList.add(wechatMoments)
        dataList.add(qq)
        rvSharePlatform.layoutManager = GridLayoutManager(activity, 4)
        rvSharePlatform.adapter = shareAdapter
        shareAdapter.update(dataList)
    }
    private fun setClickListener(){
        tvCancel.setOnClickListener{
            dismiss()
        }

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

        rlyRoot.setOnClickListener {
            dismiss()
        }
    }

    private fun saveImage(){
        val bitmap = YdFileUtils.createBitmap(poster)
        val imagePath = YdFileUtils.saveImageFromView(activity, bitmap)
        if(!TextUtils.isEmpty(imagePath)){
            Toast.makeText(activity, "图片保存成功", Toast.LENGTH_SHORT).show()
        }
        dismiss()
    }

    private fun oneKeyShare(position: Int){
        val shareAction = ShareAction(activity)
        when(position){
            1 -> {
                if(CheckApkExistUtil.checkApkExist(YdBaseApplication.context, CheckApkExistUtil.weChatPkgName)){
                    shareAction.platform = SHARE_MEDIA.WEIXIN
                }else{
                    Toast.makeText(YdBaseApplication.context, "分享失败，未安装【微信】", Toast.LENGTH_SHORT).show()
                }
            }
            2 -> {
                if(CheckApkExistUtil.checkApkExist(YdBaseApplication.context, CheckApkExistUtil.weChatPkgName)){
                    shareAction.platform = SHARE_MEDIA.WEIXIN_CIRCLE
                }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 bitmap = YdFileUtils.createBitmap(poster)
        if(bitmap != null){
            val image = UMImage(activity, bitmap)
            val thumb = UMImage(activity, bitmap)
            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
    }
}