package com.zj365.mime.act.electronicfence

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.maps.AMap
import com.amap.api.maps.AMapOptions
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.CircleOptions
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.MyLocationStyle
import com.amap.api.services.core.PoiItem
import com.hjq.toast.ToastUtils
import com.xty.base.act.BaseVmAct
import com.xty.base.vm.BaseVm
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.xty.network.model.EtFenceListBean
import com.zj365.mime.R
import com.zj365.mime.adapter.ElectronicFenceAdapter
import com.zj365.mime.databinding.ActivityElectronicFenceSettingBinding
import com.zj365.mime.vm.ElectronicFenceSettingVm

@Route(path = ARouterUrl.ELECTRONIC_FENCE_SET_ACT)
class ElectronicFenceSettingAct : BaseVmAct<ElectronicFenceSettingVm>() {

    val binding by lazy { ActivityElectronicFenceSettingBinding.inflate(layoutInflater) }

    val mAdapter by lazy { ElectronicFenceAdapter() }

    var type: Int = 0 //0 列表页 1 设置页  2 修改已设计的电子围栏

    private var aMap: AMap? = null

    var poiItem:PoiItem? = null

    var bean: EtFenceListBean.EtFenceChildBean? = null

    var total :Int = 0



    override fun liveObserver() {
        mViewModel.electronicFenceListLiveDate.observe(this){
            if(it.data.count == 0){
                binding.inEmpty.root.visibility = View.VISIBLE
                binding.rxData.visibility = View.GONE

            }else{
                binding.rxData.visibility = View.VISIBLE
                binding.inEmpty.root.visibility = View.GONE
                mAdapter.setNewInstance(it.data.list)
                total = it.data.count
                binding.tvCount.text ="${it.data.count}/5"
                var boundsBuilder = LatLngBounds.Builder()

                it.data.list.forEach {
                    var latLng = LatLng(it.lat,it.log)
                    var options = MarkerOptions().position(latLng)
                   var  radius = it.radius.toDouble()

                    boundsBuilder.include(latLng)
                    options.visible(true)
                    if(it.type == 1){
                        aMap?.let {
                            var markerOptions = MarkerOptions().position(latLng)
                                .icon(BitmapDescriptorFactory.fromView(View.inflate(this,R.layout.layout_bt_fence_enter,null))).anchor(0.5f,0.5f)
                            var marker = it.addMarker(markerOptions)

                            it.addCircle(CircleOptions().center(latLng)
                                .radius(radius).fillColor(Color.parseColor("#66F04D42"))
                                .strokeColor(Color.parseColor("#FFFFFF"))
                                .strokeWidth(1.0f)
                            )
                        }
                    }else{
                        aMap?.let {
                            var markerOptions = MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromView(View.inflate(this,R.layout.layout_bt_fence_leave,null)))
                            var marker = it.addMarker(markerOptions)

                            it.addCircle(CircleOptions().center(latLng)
                                .radius(radius.toDouble()).fillColor(Color.parseColor("#61FFAC53"))
                                .strokeColor(Color.parseColor("#FFFFFF"))
                                .strokeWidth(1.0f)
                            )
                        }
                    }
                }

                aMap?.animateCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(),50))
            }
        }

        mViewModel.addOrDeleteLiveData.observe(this){
            finish()
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "电子围栏设置"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        aMap = binding.map.map

        binding.llData.visibility = View.VISIBLE
        binding.llSetting.visibility = View.GONE

        mViewModel.getElectronicFenceList()
        binding.tvCancel.visibility = View.GONE


        binding.recycler.adapter = mAdapter
        binding.recycler.layoutManager = LinearLayoutManager(this)

        mAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as EtFenceListBean.EtFenceChildBean
            bundle.clear()
            bundle.putParcelable("bean",bean)
            bundle.putInt("type",2)
            RouteManager.goAct(ARouterUrl.ELECTRONIC_ADD_MODIFY_ACT,bundle)
        }


        binding.tvConfirm.setOnClickListener {
            if(total < 5){
                RouteManager.goAct(ARouterUrl.ELECTRONIC_ADDRESS_CHOOSE_ACT)

            }else{
                CommonToastUtils.showToast("最多可添加5个围栏")
            }

        }

        binding.tvCancel.setOnClickListener {
            if(type == 2){
                bean?.let {
                    mViewModel.deleteBtFence(it.id)
                }
            }else{
                finish()
            }
        }
        initLocation()

    }

    fun initLocation(){
        var locationStyle = MyLocationStyle()
        locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE)
        locationStyle.interval(2000)
        aMap?.let {
            it.clear()
            it.myLocationStyle = locationStyle
            it.uiSettings.isMyLocationButtonEnabled = false
            it.isMyLocationEnabled = false

            var uiSettings = it.uiSettings
            uiSettings.isZoomControlsEnabled = false
            uiSettings.isMyLocationButtonEnabled = false
            uiSettings.isScaleControlsEnabled = false
            uiSettings.logoPosition = AMapOptions.LOGO_MARGIN_LEFT
        }
    }

    override fun setLayout() = binding.root

    override fun onDestroy() {
        super.onDestroy()
        binding.map.onDestroy()
    }

    override fun onPause() {
        super.onPause()
        binding.map.onPause()
    }


    override fun onResume() {
        super.onResume()
        binding.map.onResume()
        if(type == 0){
            mViewModel.getElectronicFenceList()
        }
    }

    @SuppressLint("MissingSuperCall")
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.map.onSaveInstanceState(outState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.map.onCreate(savedInstanceState)
    }

}