package cn.sagacloud.android.cadengine

import android.graphics.PointF
import android.util.Log
import cn.sagacloud.android.cadengine.types.*
import com.alibaba.fastjson.PropertyNamingStrategy
import com.sybotan.android.graphy.SGraphyScene
import com.sybotan.base.extensions.toJson
import com.sybotan.base.utils.SHttpUtil
import com.sybotan.base.utils.SJsonUtil
import android.text.TextUtils
import android.util.SizeF
import cn.sagacloud.android.cadengine.items.*
import cn.sagacloud.android.cadengine.test.GraphyFmItem
import com.sybotan.android.graphy.events.SGraphyViewMoveEvent
import com.sybotan.android.graphy.items.SGraphyClockItem
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 楼层场景
 *
 * @author  庞利祥(sybotan@126.com)
 */
open class FloorScene : SGraphyScene() {
    /** item 数据  */
    var data: FloorData? = null
        set(value) {
            addBaseMapItem(value!!)
            field = value
        } // Set data

    /** 初始化 */
    init {
        //addItem(SGraphyClockItem(SizeF(500f, 500f), null))
    } // Init()

    /** 是否显示墙体  */
    var isShowColumn: Boolean = true
        set(value) {
            field = value
            for (column in columnList) {
                column.isVisible = value
            }
        } // Set isShowColumn

    /** 是否显示墙体  */
    var isShowWall: Boolean = true
        set(value) {
            field = value
            for (wall in wallList) {
                wall.isVisible = value
            }
        } // Set isShowWall
    /** 是否显示虚拟墙  */
    var isShowVirtualWall: Boolean = true
        set(value) {
            field = value
            for (wall in virtualWallList) {
                wall.isVisible = value
            }
        } // Set isShowWall

    /** 是否显示空间  */
    var isShowSpace: Boolean = true
        set(value) {
            field = value
            for (space in spaceList) {
                space.isVisible = value
            }
        } // Set isShowSpace

    /** 是否显示门  */
    var isShowDoor: Boolean = true
        set(value) {
            field = value
            for (door in doorList) {
                door.isVisible = value
            }
        } // Set isShowDoor

    /** 是否显示窗户  */
    var isShowWindow: Boolean = true
        set(value) {
            field = value
            for (window in windowList) {
                window.isVisible = value
            }
        } // Set isShowWindow

    /** 墙 list   */
    val wallList = ArrayList<WallItem>()
    /** 虚拟墙 list   */
    val virtualWallList = ArrayList<VirtualWallItem>()
    /** 柱子 list   */
    var columnList = ArrayList<ColumnItem>()
    /** 空间 list   */
    var spaceList = ArrayList<SpaceItem>()
    /** 空间名称 list   */
    val spaceNameList = ArrayList<SpatialCenterItem>()
    /** 门 list   */
    var doorList = ArrayList<DoorItem>()
    /** 空间 list   */
    var windowList = ArrayList<WindowItem>()

    /**
     *  获取底图压缩文件
     *
     *  @param  url         请求数据文件路径
     *  @param  isGzip      是否为压缩格式
     */
    fun loadUrl(url: String, isGzip: Boolean = true){
        Log.e("PLX","url = $url")
        try {
            val json = if (isGzip) {
                SHttpUtil.getZipRequest(url)
            } else {
                SHttpUtil.getRequest(url)
            }
//            SJsonUtil.propertyNamingStrategy = PropertyNamingStrategy.PascalCase
            val d = SJsonUtil.fromJson<FloorData>(json)
            Log.e("PLX", "json = ${d?.toJson()}")
            data = SJsonUtil.fromJson(json, FloorData::class.java)
            Log.e("数据源", "json = ${d?.toJson()}")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } // Function loadUrl()

    fun loadData(json : FloorData){
        try {
//            SJsonUtil.propertyNamingStrategy = PropertyNamingStrategy.PascalCase
            Log.e("PLX", "json = ${json?.toJson()}")
            data = json
            Log.e("数据源", "json = ${json?.toJson()}")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } // Function loadUrl()











    /**
     *  增添所有底图 item;
     *
     *  @param  data    itemList对象
     */
    private fun addBaseMapItem(data: FloorData){
        if (data.entityList.size < 1) {
            return;
        }
        val elements = data.entityList[0].elements ?: return

        // 添加墙
        if (elements.walls != null) {
            for (wall in elements.walls!!) {
                addWall(wall);
            }
        }

        // 添加虚拟墙
        if (elements.virtualWalls != null) {
            for (wall in elements.virtualWalls!!) {
                addVirtualWall(wall);
            }
        }

        // 添加柱子
        if (elements.columns != null) {
            for (column in elements.columns!!) {
                addColumn(column);
            }
        }

        // 添加空间
        if (elements.spaces != null) {
            for (space in elements.spaces!!) {
                addSpace(space)
            }
        }

        // 添加空间名称
        if (elements.spaces != null) {
            for (space in elements.spaces!!) {
                addSpaceName(space)
            }
        }

        // 添加门
        if (elements.doors != null) {
            for (door in elements.doors!!) {
                addDoor(door);
            }
        }

        // 添加窗户
        if (elements.windows != null) {
            for (window in elements.windows!!) {
                addWindow(window);
            }
        }
    } // Function addBaseMapItem()

    /**
     *  添加柱子到 scene 中
     *
     *  @param  column   柱子
     */
    open fun addColumn(column: Column){
        val item = ColumnItem(column)
        item.isVisible = isShowColumn
        columnList.add(item)
        addItem(item)
    } // Function addColumn()

    /**
     *  添加墙到 scene 中
     *
     *  @param  wall   墙
     */
    open fun addWall(wall: Wall){
        val item = WallItem(wall)
        item.isVisible = isShowWall
        wallList.add(item)
        addItem(item)
    } // Function addWall()

    /**
     *  添加所有虚拟墙到 scene 中
     *
     *  @param  wall   墙
     */
    open fun addVirtualWall(wall: VirtualWall){
        val item = VirtualWallItem(wall)
        item.isVisible = isShowVirtualWall
        virtualWallList.add(item)
        addItem(item)
    } // Function addVirtualWall()

    /**
     *  添加空间到 scene 中
     *
     *  @param  space       空间
     */
    open fun addSpace(space: Space){
        val item = SpaceItem(space)
        Log.e("打印数据",space.toJson())
        item.isVisible = isShowSpace
        spaceList.add(item)
        addItem(item)
    } // Function addSpace()

    /**
     *  添加空间名称 scene 中
     *
     *  @param  space       空间
     */
    open fun addSpaceName(space: Space){
            if (space.outLine!=null){
                val scitem= SpatialCenterItem(space.name!!)
                scitem.moveTo(PointF(space.location!!.points!![0].x, -space.location!!.points!![0].y))
                scitem.zOrder= 100f
                scitem.isVisible = true
                spaceNameList.add(scitem)
                this.addItem(scitem)

        }
    } // Function addSpace()

    /**
     *  添加门到 scene 中
     *
     *  @param  door        门
     */
    open fun addDoor(door: Door){
        val item = DoorItem(door)
        item.isVisible = isShowSpace
        doorList.add(item)
        addItem(item)
    } // Function addWindow()



    /**
     *  添加窗户到 scene 中
     *
     *  @param  window     窗户
     */
    open fun addWindow(window: Window){
        val item = WindowItem(window)
        item.isVisible = isShowSpace
        windowList.add(item)
        addItem(item)
    } // Function addWindow()

    /**
     * 截断输出日志
     * 解决Log打印不全的问题
     */
    fun println1(msg: String) {
        var msg = msg
        if (TextUtils.isEmpty(msg))
            return
        val segmentSize = 3 * 1024
        val length = msg.length.toLong()
        if (length <= segmentSize) {// 长度小于等于限制直接打印
            Log.e("PLX", msg)
        } else {
            while (msg.length > segmentSize) {// 循环分段打印日志
                val logContent = msg.substring(0, segmentSize)
                msg = msg.replace(logContent, "")
                Log.e("PLX", logContent)
            }
            Log.e("PLX", msg)// 打印剩余日志
        }
    }

    /**
     *  接收移动坐标
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMoveEvent(event: SGraphyViewMoveEvent) {
        Log.e("x",event.x.toString())
        Log.e("y",event.y.toString())
//        mapToScene = this.mapToScene( PointF(this.width/2f,this.height/2f))
//        if (cadViewMovePointF!=null){
//            cadViewMovePointF!!.viewMovePointF(mapToScene)
//        }
    }

} // Class FloorScene