/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.sybotan.android.demo.activities

import android.graphics.BitmapFactory
import android.graphics.PointF
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.SizeF
import cn.sagacloud.android.cadengine.FloorScene
import cn.sagacloud.cadengine.HttpDownloadMap
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.sybotan.android.demo.R
import com.sybotan.android.demo.R.id.graphyView
import com.sybotan.android.demo.entitiies.Revit
import com.sybotan.android.demo.graphy.GraphyWallItem
import com.sybotan.android.graphy.SGraphyScene
import com.sybotan.android.graphy.events.SGraphyViewMoveEvent
import com.sybotan.android.graphy.items.SGraphyClockItem
import com.sybotan.base.utils.SJsonUtil
import kotlinx.android.synthetic.main.activity_graphy.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

class GraphyActivity : AppCompatActivity() {
    companion object {
        private val TAG = GraphyActivity::class.java.name
    }

    val scene = FloorScene()
    val path = "base/cbde057b91e711eb9901557a780001bf.jsonz"

    @Subscribe
    fun test(msg: SGraphyViewMoveEvent) {
        Log.d(TAG, "msg=$msg")

    } // test()

    /**
     * 创建Activity时的回调函数
     *
     * @param   savedInstanceState
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_graphy)
        // 注册eventbus
        EventBus.getDefault().register(this)

        graphyView.scene = scene

        /** 开启异步 */
        doAsync {
//            scene.loadUrl("http://adm.sagacloud.cn/image-service/common/file_get?systemId=revit&key=base/61a99fc1a66b11eb8f633d7605ae9def.jsonz")
//            scene.loadUrl("http://api.sagacloud.cn/dp-auxiliary/image-service/common/file_get?systemId=revit&key=Fl42010500018b580334dcf111e8a553db1eadfac99320190803114958bim.jsonz",false)
            val downloadGZIPMap =
                HttpDownloadMap.downloadGZIPMap(path)
            if (downloadGZIPMap != null) {
                scene.loadData(downloadGZIPMap!!)
            }
//            val graphyFmItem = GraphyFmItem()
//            graphyFmItem.show()
//            graphyFmItem.isVisible = true
//            graphyFmItem.moveTo(300f,300f)
////            graphyFmItem.rotate = 60f // (20f*Math.PI / 180f) as Float
////            graphyFmItem.isTransform = false
//            scene.addItem(graphyFmItem)
//            val graphyTwoItem = GraphyTwoItem()
//            graphyTwoItem.parent = graphyFmItem
//            //graphyTwoItem.scale = PointF(2.0f,2.0f)
////            graphyTwoItem.rotate = 60f
//            Log.e("children" ,graphyFmItem.children.size.toString())
////            graphyTwoItem.show()
////            graphyTwoItem.flag = true
////            graphyTwoItem.parent = graphyFmItem
//            graphyTwoItem.moveTo(100f,100f)
//            graphyTwoItem.color = Color.RED


//            scene.addItem(graphyTwoItem)
            /** 主线程处理ui */
            uiThread {


                /** 适应屏幕大小 */
                graphyView.fitSceneToView()
                /** 限定移动范围不超出屏幕 */
//                graphyView.moveRange()
                graphyView.post {
                    Log.e("viewW", graphyView.width.toString())
                }

                Log.e("viewH", graphyView.measuredHeight.toString())
            }
        }

        return
    } // Function onCreate()

    /**
     * 加载墙数据
     */
    private fun loadWalls() {
//        val revit = SJsonUtil.fromJson<Revit>(applicationContext.assets.open("WallDatas.json"), PropertyNamingStrategy.UPPER_CAMEL_CASE)
//        Log.d(TAG, "name=${revit.name}")
//        Log.d(TAG, "${revit.floorDatas!![revit.floorDatas!!.size - 1].lines!!}")
//
//        Log.d(TAG, "line.size=${revit.floorDatas!![revit.floorDatas!!.size - 1].lines!!.size}")
//        //for ( line in revit.floorDatas!![revit.floorDatas!!.size - 1].lines!!) {
//        for ( line in revit.floorDatas!![0].lines!!) {
//            val pointList = ArrayList<PointF>()
//            Log.d(TAG, "point.size=${line.size}")
//            for (t in 0 until  line.size) {
//                val p1 = line[t].split(",")
//                val point = PointF(p1[0].toFloat() * 20  + 12000, -p1[1].toFloat() * 20  -20600)
//                pointList.add(point)
//            }
//            scene.addItem(GraphyWallItem(pointList))
//        }
//        println()

        return
    } // Function loadWalls()

} // Class GraphyActivity
