package com.ys.bdtp.adm.mvvm.ui.examine

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.cardview.widget.CardView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.ys.bdtp.adm.R
import com.ys.bdtp.adm.mvvm.model.vo.Object
import com.ys.bdtp.adm.mvvm.model.vo.*
import com.ys.bdtp.adm.mvvm.vm.offline.OfflineViewModel
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.closestDI
import org.kodein.di.instance

class Space3DActivity : ComponentActivity(), DIAware, View.OnClickListener {

    override val di: DI by closestDI()
    val vm by instance<OfflineViewModel>()
    var isLastSpace = false
    var isLockCamera = false
    var meshObject: MeshObjectModel? = null
    private lateinit var buildingId: String
    private lateinit var floorId: String
    private lateinit var projectId: String
    private var objTypeCode: String = ""
    private var problemTypeCode: String = ""
    private lateinit var floorEntity: Object
    private lateinit var tv2d: TextView
    private lateinit var tv3d: TextView
    private lateinit var compass: TextView
    private lateinit var vertical: TextView
    private lateinit var degree: TextView
    private lateinit var lock: TextView
    private lateinit var angle: TextView
    private lateinit var chooseObject: TextView
    private lateinit var chooseSpace: TextView
    private lateinit var wall: TextView
    private lateinit var door: TextView
    private lateinit var pillar: TextView
    private lateinit var window: TextView
    private lateinit var parking: TextView
    private lateinit var floorslab: TextView
    private lateinit var addProblem: TextView
    private lateinit var overall: TextView
    private lateinit var forwardTools: LinearLayout
    private lateinit var reverseTools: LinearLayout
    private lateinit var drawTools: LinearLayout
    private lateinit var quitTools: LinearLayout
    private lateinit var mapTools: LinearLayout
    private lateinit var cameraTypeTools: LinearLayout
    private lateinit var point: TextView
    private lateinit var pline: TextView
    private lateinit var close: TextView
    private lateinit var save: TextView
    private lateinit var record: TextView
    private lateinit var delete: TextView
    private lateinit var quit: TextView
    private lateinit var problemDone: TextView

    private lateinit var less: TextView
    private lateinit var more: TextView
    private lateinit var materialError: TextView
    private lateinit var propertyError: TextView
    private lateinit var crossSectionError: TextView
    private lateinit var sizeError: TextView
    private lateinit var positionError: TextView
    private lateinit var floorslabLess: TextView
    private lateinit var plantHoleLess: TextView
    private lateinit var floorslabHeightError: TextView
    private lateinit var signProblem: CardView
    private lateinit var closeProblem: ImageView

    companion object {
        private lateinit var webview: WebView

        /*lateinit var viewModel: Space3DVM*/
        val gson = Gson()


        /**
         * 启动 Space3DActivity
         */
        fun startSpace3DActivity(context: Context, projectId: String) {
            val intent = Intent(context, Space3DActivity::class.java)
            intent.putExtra("projectId", projectId)
            context.startActivity(intent)
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_space3d_activity)
        /* val floor = intent.getBundleExtra("floor")!!
         buildingId = floor.getString("buildingId", "")
         floorId = floor.getString("floorId", "")
         projectId = floor.getString("projectId", "")*/
        projectId = intent.getStringExtra("projectId") ?: ""
        initView()
        webview.clearCache(true)
        WebView.setWebContentsDebuggingEnabled(true)
        webview.settings.apply {
            javaScriptEnabled = true
            defaultTextEncodingName = "utf-8"
            builtInZoomControls = true
            loadsImagesAutomatically = true
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            //模式浏览界面，当页面宽度超过WebView显示宽度时，缩小页面适应WebView。默认false
            loadWithOverviewMode = true
            domStorageEnabled = true
            databaseEnabled = true
            mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        webview.addJavascriptInterface(this, "android")
        webview.webViewClient = MyWebViewClient()
        webview.loadUrl("http://60.205.177.43:8000/APP/")
//        webview.loadUrl("file:///android_asset/dist/index.html")


        /*viewModel = Space3DVM(this.application, { name, param ->
            when (name) {
                Space3DVM.FLOOR_SPACE -> {
                    val spaces = param as ArrayList<ObjectEntity>
                    val spaceDataList = ArrayList<SpaceData3D>()
                    val space3D = SpaceData3D()
                    for (space in spaces) {
                        val id = SpaceData3D.ID()
                        id.ID = space.id
                        val info = gson.fromJson(space.infos, SpaceInfo::class.java)
                        val outLineString = gson.toJson(info.outline)
                        id.OutLine = outLineString.replace("\"", "\\\"")
                        val floorInfo = gson.fromJson(floorEntity.infos, InfosforFloor::class.java)
                        id.FloorElevation = floorInfo.floorElevation
                        space3D.ID.add(id)
                        if (space3D.ID.size >= 5 || space == spaces[spaces.size - 1]) {
                            if (space === spaces[spaces.size - 1]) {
                                Thread.sleep(300)
                                isLastSpace = true
                            }
                            spaceDataList.add(space3D)
                            val sss = gson.toJson(spaceDataList)
                            webview.loadUrl("javascript:SetSpaceData('${sss}')")
                            space3D.ID.clear()
                            spaceDataList.clear()
                        }
                    }


                }
                Space3DVM.FLOOR_OBJECT -> {
                    floorEntity = param as ObjectEntity
                }
            }
        }, this, projectId, buildingId, floorId)*/
    }

    private fun initView() {
        webview = findViewById(R.id.webview)
        tv2d = findViewById(R.id.tv2d)
        tv3d = findViewById(R.id.tv3d)
        compass = findViewById(R.id.compass)
        vertical = findViewById(R.id.vertical)
        degree = findViewById(R.id.degree)
        lock = findViewById(R.id.lock)
        angle = findViewById(R.id.angle)
        chooseObject = findViewById(R.id.chooseObject)
        chooseSpace = findViewById(R.id.chooseSpace)
        reverseTools = findViewById(R.id.reverseTools)
        forwardTools = findViewById(R.id.forwardTools)
        wall = findViewById(R.id.wall)
        door = findViewById(R.id.door)
        pillar = findViewById(R.id.pillar)
        window = findViewById(R.id.window)
        parking = findViewById(R.id.parking)
        floorslab = findViewById(R.id.floorslab)
        more = findViewById(R.id.more)
        propertyError = findViewById(R.id.propertyError)
        positionError = findViewById(R.id.positionError)
        addProblem = findViewById(R.id.addProblem)
        overall = findViewById(R.id.overall)
        drawTools = findViewById(R.id.drawTools)
        point = findViewById(R.id.point)
        pline = findViewById(R.id.pline)
        close = findViewById(R.id.close)
        save = findViewById(R.id.save)
        record = findViewById(R.id.record)
        delete = findViewById(R.id.delete)
        quitTools = findViewById(R.id.quitTools)
        quit = findViewById(R.id.quit)
        problemDone = findViewById(R.id.problemDone)
        mapTools = findViewById(R.id.mapTools)
        cameraTypeTools = findViewById(R.id.cameraTypeTools)
        less = findViewById(R.id.less)
        more = findViewById(R.id.more)
        materialError = findViewById(R.id.materialError)
        propertyError = findViewById(R.id.propertyError)
        crossSectionError = findViewById(R.id.crossSectionError)
        sizeError = findViewById(R.id.sizeError)
        positionError = findViewById(R.id.positionError)
        floorslabLess = findViewById(R.id.floorslabLess)
        plantHoleLess = findViewById(R.id.plantHoleLess)
        floorslabHeightError = findViewById(R.id.floorslabHeightError)
        signProblem = findViewById(R.id.signProblem)
        closeProblem = findViewById(R.id.closeProblem)

        less.setOnClickListener(this)
        more.setOnClickListener(this)
        materialError.setOnClickListener(this)
        propertyError.setOnClickListener(this)
        crossSectionError.setOnClickListener(this)
        sizeError.setOnClickListener(this)
        positionError.setOnClickListener(this)
        floorslabLess.setOnClickListener(this)
        plantHoleLess.setOnClickListener(this)
        floorslabHeightError.setOnClickListener(this)
        tv2d.setOnClickListener(this)
        tv3d.setOnClickListener(this)
        compass.setOnClickListener(this)
        vertical.setOnClickListener(this)
        degree.setOnClickListener(this)
        lock.setOnClickListener(this)
        angle.setOnClickListener(this)
        chooseObject.setOnClickListener(this)
        chooseSpace.setOnClickListener(this)
        wall.setOnClickListener(this)
        door.setOnClickListener(this)
        pillar.setOnClickListener(this)
        window.setOnClickListener(this)
        parking.setOnClickListener(this)
        floorslab.setOnClickListener(this)
        more.setOnClickListener(this)
        propertyError.setOnClickListener(this)
        positionError.setOnClickListener(this)
        addProblem.setOnClickListener(this)
        overall.setOnClickListener(this)
        quit.setOnClickListener(this)
        closeProblem.setOnClickListener(this)
        save.setOnClickListener(this)
    }


    @JavascriptInterface
    fun MeshClick(meshObjectString: String) {
        runOnUiThread {
            forwardTools.visibility = View.GONE
            reverseTools.visibility = View.GONE
            meshObject = gson.fromJson(meshObjectString, MeshObjectModel::class.java)
            Toast.makeText(this, meshObject!!.ID, Toast.LENGTH_LONG).show()
        }
    }

    @JavascriptInterface
    fun EndDrawPointModel(pointModel: String) {
        runOnUiThread {
            val point = gson.fromJson(pointModel, PointZ::class.java)
        }
    }

    @JavascriptInterface
    fun EndDrawLineModel(lineObject: String) {
        runOnUiThread {
            val line = gson.fromJson<ArrayList<PointZ?>?>(lineObject, object : TypeToken<ArrayList<PointZ?>?>() {}.type)
        }
    }

    @JavascriptInterface
    fun FunctionEndBlock(name: String) {
        val nameObject = gson.fromJson(name, FunctionEndBlockModel::class.java)
        when (nameObject.Name) {
            "OpenPage" -> {
                runOnUiThread {
                    webview.loadUrl("javascript:OpenTestData()")
                }
                /*runOnUiThread { viewModel.getFloorSpace() }*/
            }
            "SetSpaceData" -> {
//                if (isLastSpace) {
//                    runOnUiThread {
//                        val floorInfo = gson.fromJson(floorEntity.infos, InfosforFloor::class.java)
//                        val model = SetFloorPathModel(projectId, floorInfo.bimIdPre, floorInfo.floorElevation)
//                        webview.loadUrl("javascript:SetFloorPath('${gson.toJson(model)}')")
//                    }
//                }
            }
            "SetFloorPath" -> {
//                runOnUiThread {
//                    webview.loadUrl("javascript:LoadAllModelWithFloor()")
//                }
            }
            "OpenTestData" -> {
                runOnUiThread {
                    webview.loadUrl("javascript:LoadAllModelWithFloor()")

                }
            }
        }
    }

    class MyWebViewClient : WebViewClient() {
        //兼容低版本
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }

        override fun onPageFinished(view: WebView, url: String) {
            super.onPageFinished(view, url)
            if (view.progress == 100) {
//            viewModel.getFloorSpace()
                val pageParam = PageParam()
                val param = PageParam.Param()
                param.PageType = "1"
                pageParam.Param = param
                webview.loadUrl("javascript:OpenPage('${gson.toJson(pageParam)}')")
            }
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv2d -> {
                meshObject = null
                webview.loadUrl("javascript:UpdateCameraType(${2})")
                tv2d.background = getDrawable(R.drawable.bg_blue_round)
                tv3d.background = getDrawable(R.color.trans)
                reset2DTools()
            }
            R.id.tv3d -> {
                meshObject = null
                webview.loadUrl("javascript:UpdateCameraType(${1})")
                tv3d.background = getDrawable(R.drawable.bg_blue_round)
                tv2d.background = getDrawable(R.color.trans)
                reset3DTools()
            }
            R.id.compass -> {
//                compass.setCompoundDrawablesWithIntrinsicBounds(null, resources.getDrawable(R.drawable.icon_compass_blue), null, null)
            }
            R.id.overall -> {
                webview.loadUrl("javascript:LookAtAllFloorWidth2D()")
                overall.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_overall_blue),
                    null,
                    null
                )
            }
            R.id.vertical -> {
                webview.loadUrl("javascript:UpdateCameraAngle(${0})")
                resetState()
                vertical.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_vertical_blue),
                    null,
                    null
                )
            }
            R.id.degree -> {
                webview.loadUrl("javascript:UpdateCameraAngle(${1})")
                resetState()
                degree.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_degree_blue),
                    null,
                    null
                )
            }
            R.id.lock -> {
                isLockCamera = !isLockCamera
                webview.loadUrl("javascript:IsLockCamera(${isLockCamera})")
                lock.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    if (isLockCamera) resources.getDrawable(R.drawable.icon_lock_blue) else resources.getDrawable(R.drawable.icon_lock),
                    null,
                    null
                )

            }
            R.id.angle -> {
                resetState()
                webview.loadUrl("javascript:IsFirstCamera(${true})")
                angle.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_angle_blue),
                    null,
                    null
                )
            }
            R.id.chooseObject -> {
                webview.loadUrl("javascript:SetChoseBuildType()")
                meshObject = null
                chooseObject.setTextColor(getColor(R.color.blue_0091FF))
                chooseObject.background = getDrawable(R.drawable.btn_bg_blue_left_close)
                chooseSpace.setTextColor(getColor(R.color.black_1F2329))
                chooseSpace.background = getDrawable(R.drawable.btn_bg_gray_right_open)
            }
            R.id.chooseSpace -> {
                webview.loadUrl("javascript:SetChoseSpaceType()")
                meshObject = null
                chooseSpace.setTextColor(getColor(R.color.blue_0091FF))
                chooseSpace.background = getDrawable(R.drawable.btn_bg_blue_right_close)
                chooseObject.setTextColor(getColor(R.color.black_1F2329))
                chooseObject.background = getDrawable(R.drawable.btn_bg_gray_left_open)
            }
            R.id.wall -> {
                objTypeCode = "CFCSWL"
                hideForwardTools()
                less.visibility = View.VISIBLE
                more.visibility = View.VISIBLE
                materialError.visibility = View.VISIBLE
                pline.visibility = View.VISIBLE
                pline.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_pline_blue),
                    null,
                    null
                )
                resetReverseTools()
                wall.setTextColor(getColor(R.color.blue_0091FF))
                wall.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            R.id.door -> {
                objTypeCode = "CFBEDR"
                problemTypeCode = "CFBEDR_LESS"
                resetReverseTools()
                resetDrawTools()
                webview.loadUrl("javascript:InDrawPointModel()")
                point.visibility = View.VISIBLE
                point.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_point_blue),
                    null,
                    null
                )
                door.setTextColor(getColor(R.color.blue_0091FF))
                door.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            R.id.pillar -> {
                objTypeCode = "CFCSCL"
                problemTypeCode = "CFCSCL_LESS"
                resetReverseTools()
                resetDrawTools()
                webview.loadUrl("javascript:InDrawPointModel()")
                point.visibility = View.VISIBLE
                point.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_point_blue),
                    null,
                    null
                )
                pillar.setTextColor(getColor(R.color.blue_0091FF))
                pillar.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            R.id.window -> {
                objTypeCode = "CFBEWN"
                problemTypeCode = "CFBEWN_LESS"
                resetReverseTools()
                resetDrawTools()
                webview.loadUrl("javascript:InDrawPointModel()")
                point.visibility = View.VISIBLE
                point.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_point_blue),
                    null,
                    null
                )
                window.setTextColor(getColor(R.color.blue_0091FF))
                window.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            R.id.parking -> {
                objTypeCode = "CFPKPS"
                resetReverseTools()
                resetDrawTools()
                webview.loadUrl("javascript:InDrawPointModel()")
                point.visibility = View.VISIBLE
                point.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    resources.getDrawable(R.drawable.icon_point_blue),
                    null,
                    null
                )
                parking.setTextColor(getColor(R.color.blue_0091FF))
                parking.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            R.id.floorslab -> {
                objTypeCode = "CFCSFL"
                hideForwardTools()
                floorslabLess.visibility = View.VISIBLE
                plantHoleLess.visibility = View.VISIBLE
                floorslabHeightError.visibility = View.VISIBLE
                resetReverseTools()
                floorslab.setTextColor(getColor(R.color.blue_0091FF))
                floorslab.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            R.id.addProblem -> {
                if (reverseTools.visibility == View.VISIBLE || forwardTools.visibility == View.VISIBLE) {
                    forwardTools.visibility = View.GONE
                    reverseTools.visibility = View.GONE
                } else {
                    if (meshObject == null) {
                        reverseTools.visibility = View.VISIBLE
                        forwardTools.visibility = View.GONE
                    } else {
                        hideForwardTools()
                        if (meshObject?.ID!!.contains("Column")) {
                            objTypeCode = "CFCSCL"
                            more.visibility = View.VISIBLE
                            crossSectionError.visibility = View.VISIBLE
                            sizeError.visibility = View.VISIBLE

                        } else if (meshObject?.ID!!.contains("Door")) {
                            objTypeCode = "CFBEDR"
                            more.visibility = View.VISIBLE
                            materialError.visibility = View.VISIBLE
                            positionError.visibility = View.VISIBLE
                        } else if (meshObject?.ID!!.contains("Window")) {
                            objTypeCode = "CFBEWN"
                            more.visibility = View.VISIBLE
                            propertyError.visibility = View.VISIBLE
                            sizeError.visibility = View.VISIBLE
                            positionError.visibility = View.VISIBLE
                        } else if (meshObject?.ID!!.contains("Parking")) {
                            problemTypeCode = "CFPKPS_LESS"
                            objTypeCode = "CFPKPS"
                            more.visibility = View.VISIBLE
                        }
                    }
                }
            }
            R.id.quit -> {
                webview.loadUrl("javascript:OutDrawPointModel()")
                webview.loadUrl("javascript:OutDrawLineModel()")
                signProblem.visibility = View.GONE
                addProblem.visibility = View.VISIBLE
                drawTools.visibility = View.GONE
                quitTools.visibility = View.GONE
                problemDone.visibility = View.GONE
                cameraTypeTools.visibility = View.VISIBLE
                mapTools.visibility = View.VISIBLE
                webview.loadUrl("javascript:UpdateCameraType(${1})")
                resetReverseTools()
            }
            R.id.problemDone -> {
                //TODO save
                webview.loadUrl("javascript:OutDrawPointModel()")
                webview.loadUrl("javascript:OutDrawLineModel()")
                webview.loadUrl("javascript:EndDrawPointModel()")
                webview.loadUrl("javascript:EndDrawLineModel()")
            }
            R.id.save -> {
                if (problemTypeCode == "CFCSWL_MORE" || problemTypeCode == "CFCSWL_MATERIAL_ERROR" || objTypeCode == "CFCSFL") {
//                    val lineStyleModel = LineStyleModel("#ff0000", true)
                    val lineStyleModel = "{'Color':'#ff0000', 'IsLoop':'true'}"
                    webview.loadUrl("javascript:SetDrawLineModel(${lineStyleModel})")
                }
//                webview.loadUrl("javascript:OutDrawPointModel()")
//                webview.loadUrl("javascript:OutDrawLineModel()")
//                webview.loadUrl("javascript:EndDrawPointModel()")
//                webview.loadUrl("javascript:EndDrawLineModel()")
            }
            //缺少
            R.id.less -> {
                when (objTypeCode) {
                    //墙
                    "CFCSWL" -> {
                        problemTypeCode = "CFCSWL_LESS"
                        webview.loadUrl("javascript:InDrawLineModel()")
                        resetDrawTools()
                    }
                }
            }
            //多余
            R.id.more -> {
                when (objTypeCode) {
                    //墙
                    "CFCSWL" -> {
                        problemTypeCode = "CFCSWL_MORE"
                        webview.loadUrl("javascript:InDrawLineModel()")
                        resetDrawTools()
                    }
                    //柱子
                    "CFCSCL" -> {
                        problemTypeCode = "CFCSCL_MORE"
                        signProblem.visibility = View.VISIBLE
                        addProblem.visibility = View.GONE
                        forwardTools.visibility = View.GONE
                    }
                    //门
                    "CFBEDR" -> {
                        problemTypeCode = "CFBEDR_MORE"
                        signProblem.visibility = View.VISIBLE
                        addProblem.visibility = View.GONE
                        forwardTools.visibility = View.GONE
                    }
                    //窗
                    "CFBEWN" -> {
                        problemTypeCode = "CFBEWN_MORE"
                        signProblem.visibility = View.VISIBLE
                        addProblem.visibility = View.GONE
                        forwardTools.visibility = View.GONE
                    }
                    //停车位
                    "CFPKPS" -> {
                        problemTypeCode = "CFPKPS_MORE"
                        signProblem.visibility = View.VISIBLE
                        addProblem.visibility = View.GONE
                        forwardTools.visibility = View.GONE
                    }
                }
                resetForwardTools()
            }
            //材质错误
            R.id.materialError -> {
                problemTypeCode = "CFCSWL_MATERIAL_ERROR"
                webview.loadUrl("javascript:InDrawLineModel()")
                resetDrawTools()
            }
            //属性错误
            R.id.propertyError -> {
                when (objTypeCode) {
                    //门
                    "CFBEDR" -> {
                        problemTypeCode = "CFBEDR_PROPERTY_ERROR"
                    }
                    //窗
                    "CFBEWN" -> {
                        problemTypeCode = "CFBEWN_PROPERTY_ERROR"
                    }

                }
                resetForwardTools()
                propertyError.setTextColor(getColor(R.color.blue_0091FF))
                propertyError.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            //截面形状错误
            R.id.crossSectionError -> {
                problemTypeCode = "CFCSCL_SECTION_SHAPE_ERROR"
                signProblem.visibility = View.VISIBLE
                addProblem.visibility = View.GONE
                forwardTools.visibility = View.GONE
            }
            //尺寸错误
            R.id.sizeError -> {
                when (objTypeCode) {
                    "CFCSCL" -> {
                        problemTypeCode = "CFCSCL_SIZE_ERROR"
                    }
                }
                signProblem.visibility = View.VISIBLE
                addProblem.visibility = View.GONE
                forwardTools.visibility = View.GONE
            }
            //位置错误
            R.id.positionError -> {
                when (objTypeCode) {
                    //门
                    "CFBEDR" -> {
                        problemTypeCode = "CFBEDR_POSITION_ERROR"
                    }
                    //窗
                    "CFBEWN" -> {
                        problemTypeCode = "CFBEWN_POSITION_ERROR"
                    }

                }
                resetForwardTools()
                positionError.setTextColor(getColor(R.color.blue_0091FF))
                positionError.background = getDrawable(R.drawable.bg_blue_e1f2ff_round)
            }
            //楼板缺少
            R.id.floorslabLess -> {
                problemTypeCode = "CFCSFL_LESS"
                webview.loadUrl("javascript:InDrawLineModel()")
                resetDrawTools()
                resetForwardTools()
            }
            //板洞缺少
            R.id.plantHoleLess -> {
                problemTypeCode = "CFCSFL_HOLE_LESS"
                webview.loadUrl("javascript:InDrawLineModel()")
                resetDrawTools()
                resetForwardTools()
            }
            //楼板高错误
            R.id.floorslabHeightError -> {
                problemTypeCode = "CFCSFL_ELEVATION_ERROR"
                webview.loadUrl("javascript:InDrawLineModel()")
                resetDrawTools()
                resetForwardTools()
            }
            R.id.record -> {
                signProblem.visibility = View.VISIBLE
            }
            R.id.closeProblem -> {
                signProblem.visibility = View.GONE
                addProblem.visibility = View.VISIBLE
                drawTools.visibility = View.GONE
                quitTools.visibility = View.GONE
                problemDone.visibility = View.GONE
                cameraTypeTools.visibility = View.VISIBLE
                mapTools.visibility = View.VISIBLE
            }

        }
    }

    private fun resetReverseTools() {
        wall.setTextColor(getColor(R.color.black_1F2329))
        wall.background = resources.getDrawable(R.color.trans)
        door.setTextColor(getColor(R.color.black_1F2329))
        door.background = resources.getDrawable(R.color.trans)
        pillar.setTextColor(getColor(R.color.black_1F2329))
        pillar.background = resources.getDrawable(R.color.trans)
        window.setTextColor(getColor(R.color.black_1F2329))
        window.background = resources.getDrawable(R.color.trans)
        parking.setTextColor(getColor(R.color.black_1F2329))
        parking.background = resources.getDrawable(R.color.trans)
        floorslab.setTextColor(getColor(R.color.black_1F2329))
        floorslab.background = resources.getDrawable(R.color.trans)
    }

    private fun resetForwardTools() {
        more.setTextColor(getColor(R.color.black_1F2329))
        more.background = resources.getDrawable(R.color.trans)
        propertyError.setTextColor(getColor(R.color.black_1F2329))
        propertyError.background = resources.getDrawable(R.color.trans)
        positionError.setTextColor(getColor(R.color.black_1F2329))
        positionError.background = resources.getDrawable(R.color.trans)
    }

    private fun resetState() {
        vertical.setCompoundDrawablesWithIntrinsicBounds(
            null,
            resources.getDrawable(R.drawable.icon_vertical),
            null,
            null
        )
        degree.setCompoundDrawablesWithIntrinsicBounds(
            null,
            resources.getDrawable(R.drawable.icon_degree),
            null,
            null
        )
//        lock.setCompoundDrawablesWithIntrinsicBounds(null, resources.getDrawable(R.drawable.icon_lock), null, null)
        angle.setCompoundDrawablesWithIntrinsicBounds(
            null,
            resources.getDrawable(R.drawable.icon_angle),
            null,
            null
        )
        webview.loadUrl("javascript:IsFirstCamera(${false})")
    }

    private fun reset3DTools() {
        vertical.visibility = View.VISIBLE
        overall.visibility = View.GONE
        degree.visibility = View.VISIBLE
        lock.visibility = View.VISIBLE
        angle.visibility = View.VISIBLE
        overall.setCompoundDrawablesWithIntrinsicBounds(
            null,
            resources.getDrawable(R.drawable.icon_overall_white),
            null,
            null
        )
    }

    private fun hideForwardTools() {
        forwardTools.visibility = View.VISIBLE
        less.visibility = View.GONE
        more.visibility = View.GONE
        materialError.visibility = View.GONE
        propertyError.visibility = View.GONE
        crossSectionError.visibility = View.GONE
        sizeError.visibility = View.GONE
        positionError.visibility = View.GONE
        floorslabLess.visibility = View.GONE
        plantHoleLess.visibility = View.GONE
        floorslabHeightError.visibility = View.GONE
    }

    private fun reset2DTools() {
        resetState()
        vertical.visibility = View.GONE
        overall.visibility = View.VISIBLE
        degree.visibility = View.GONE
        lock.visibility = View.GONE
        angle.visibility = View.GONE
    }

    private fun resetDrawTools() {
        webview.loadUrl("javascript:UpdateCameraType(${2})")
        addProblem.visibility = View.GONE
        forwardTools.visibility = View.GONE
        reverseTools.visibility = View.GONE
        point.visibility = View.GONE
        pline.visibility = View.GONE
        close.visibility = View.GONE
        cameraTypeTools.visibility = View.GONE
        mapTools.visibility = View.GONE
        drawTools.visibility = View.VISIBLE
        quitTools.visibility = View.VISIBLE
        problemDone.visibility = View.VISIBLE
    }
}