package com.jld.vod.service

import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.view.WindowManager
import com.google.gson.Gson
import com.jld.vod.R
import com.jld.vod.config.Config
import com.jld.vod.model.bean.GEOAdvertBean
import com.jld.vod.model.bean.ResultBean
import com.jld.vod.model.event.ReceiveEvent
import com.jld.vod.socket.ReceiveSocketManager
import com.jld.vod.socket.SendSocketManager
import com.jld.vod.utils.LogUtils
import com.jld.vod.utils.SharedPrefUtils
import com.jld.vod.utils.http.OkHttpUtils
import com.jld.vod.view.SplashActivity
import com.jld.vod.view.widget.CustomAdDialog
import com.jld.vod.view.widget.CustomBlackBgDialog
import com.jld.vod.view.widget.CustomGEOAdDialog
import com.jld.vod.view.widget.CustomScreenAdDialog
import okhttp3.Call
import okhttp3.Response
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONObject
import java.io.IOException

/**
 * upd服务
 */
class SocketService : Service() {
    private var sendSocketManager: SendSocketManager? = null
    private var receiveSocketManager: ReceiveSocketManager? = null
    private var mBgCustom : CustomBlackBgDialog? = null
    private var mGEOCustom : CustomScreenAdDialog? = null
    private var gson:Gson?= null
    private val BLACKBG : Int  =  0x10
    private val GEOAD :Int = 0x11
    private val DISGEOAD :Int = 0x12

    private val mHandler = Handler(Handler.Callback { message ->

        when(message.what)
        {
            BLACKBG->{
                val msg = message.obj.toString()
                    if (msg == "true")
                    {
                        if (mBgCustom == null)
                        {
                            mBgCustom = CustomBlackBgDialog(this)
                            mBgCustom!!.getWindow()!!.setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
                            mBgCustom!!.show()
                        }
                    }else if(msg == "false")
                    {
                        if (mBgCustom != null)
                        {
                            mBgCustom!!.dismiss()
                            mBgCustom = null
                        }
                    }
            }
            GEOAD->{
                val msgs = message.obj as GEOAdvertBean

                if (msgs.data != null)
                {
                    if (mGEOCustom == null)
                    {
                        mGEOCustom = CustomScreenAdDialog(this)
                        mGEOCustom!!.getWindow()!!.setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
                    }

                    mGEOCustom!!.setDataList(msgs.data)
                    mGEOCustom!!.show()
                }
            }
            DISGEOAD->{

                if (mGEOCustom != null)
                {
                    mGEOCustom!!.dismiss()
                    mGEOCustom = null
                }
            }
        }

                false
            })
    override fun onCreate() {
        EventBus.getDefault().register(this)

        receiveSocketManager = ReceiveSocketManager.getInstance(this)
        receiveSocketManager!!.startUdpConnection()

        //初始化gson
        gson = Gson()

        super.onCreate()
    }


    /**
     * 接收UDP广播
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceive( action : ReceiveEvent?) {
        LogUtils.logD("接收广播"+action)
        if (action!!.data == null)return
        val jsonObject = JSONObject(action!!.data)
        try {
            val type = jsonObject.optString("type")
            val msg = jsonObject.optString("msg")

            when (type)
            {
                "CLOSE"->{
                    receiveSocketManager!!.stopSocket()
                }
                Config.SCREEN->{
                    val msgs = Message()
                    msgs.what = BLACKBG
                    msgs.obj = msg
                    mHandler.sendMessage(msgs)
                }
                Config.ADS->{
                    if (msg == "true")
                    {
                        initAdVert()
                    }else if(msg == "false")
                    {
                        val msgs = Message()
                        msgs.what = DISGEOAD
                        mHandler.sendMessage(msgs)
                    }
                }
            }
        }catch (e:Exception)
        {
            e.printStackTrace()
        }
    }
    /**
     * 初始化广告模块
     */
    private fun initAdVert() {
        OkHttpUtils.getInstance().getDataAsyn(Config.ApiBaseUrl+"/advert/findPositionAdvert",object : OkHttpUtils.MyNetCall{
            override fun failed(call: Call?, e: IOException?) {
                LogUtils.logE(e.toString())
            }
            override fun success(call: Call?, response: Response?) {
                val json = response!!.body!!.string()

                val mGEOAdvertBean= gson!!.fromJson(json, GEOAdvertBean::class.java)
                val msg = Message()
                msg.what = GEOAD
                msg.obj = mGEOAdvertBean
                mHandler.sendMessage(msg)

            }
        })
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

}