package cn.sagacloud.cadengine

import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.PointF
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.text.LoginFilter
import android.util.Log
import cn.sagacloud.android.cadengine.FloorScene
import cn.sagacloud.android.cadengine.test.GraphyFmItem
import cn.sagacloud.android.cadengine.test.GraphyTwoItem
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

class MainActivity : AppCompatActivity() {
    val scene = FloorScene()
    val path = "Fl42010500018b580334dcf111e8a553db1eadfac99320190803114958bim.jsonz"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        addPermission()
        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.isTransform = false
            graphyFmItem.moveTo(300f,300f)
            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()
            }
        }
    } // Function onCreate()

    companion object {
        val CODE_PERMISSION = 1604
        private val CODE_DOWNLOAD = 1808
        private val CODE_CLEAR = 1911
    }

    /**
     * Desc: 动态请求权限
     * Created by mengxy
     * Mail: mengxiangyong@persagy.com
     * 2018/3/29 15:08
     */
    private fun addPermission() {
        val isAllGranted = checkPermissionAllGranted(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO)
        )
        if (!isAllGranted) {
            ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA,
                            Manifest.permission.RECORD_AUDIO),
                    CODE_PERMISSION
            )
        }
    }

    /**
     * 检查是否拥有指定的所有权限
     */
    private fun checkPermissionAllGranted(permissions: Array<String>): Boolean {
        // 只要有一个权限没有被授予, 则直接返回 false
        return permissions.none { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED }
    }
} // Class MainActivity
