package com.abbidot.tracker.ui.activity.device.fences

import android.view.View
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.FencesBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class EditNoGoZoneActivity : AddAndEditFencesZoneBaseActivity() {
    override fun initData() {
        setTopBarTitle(R.string.txt_edit_no_go_zone)

        isEditFences = true
        val editFences =
            Util.getParcelableAdaptive(intent, ConstantString.CurrentFence, FencesBean::class.java)
        if (null == editFences) finish()
        else mFencesBean = editFences

        super.initData()

        mFencesBean.apply {
            mFencesMapCommon.restoreEditFencesSetMapZoom(mContext, this)
            //保存反地理信息 ，下次直接使用
            mGeoCoderViewModel.mAddress = address
            mGeoCoderViewModel.mLat = latitudeCenter
            mGeoCoderViewModel.mLon = longitudeCenter
            mViewBinding.ilFencesZoneNameInput.etInputContent.setText(name)
        }

        mRightImageButton = addRightImageButton(R.drawable.icon_gray_delete_svg)
        noGoZoneInitData(true)
    }

    override fun liveDataObserve() {
        super.liveDataObserve()
        editLiveDataObserve()
    }

    override fun mapLoadOk() {
        mFencesList?.apply {
            nogoFenceList?.let {
                for (fences in it) {
                    if (mFencesBean.fenceId == fences.fenceId) {
                        it.remove(fences)
                        break
                    }
                }
            }
            drawFences()
        }

        mFencesBean.apply {
            ViewUtil.instance.updateDataBeanOnlySelectedItem(
                mFencesZoneShapeAdapter, mFencesZoneShapeList, fenceShapeType - 1
            )
            ViewUtil.instance.updateDataBeanOnlySelectedItem(
                mSelectTypeShadeAdapter, mFencesZoneTypeList, fenceNameType - 6
            )
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        mViewBinding.apply {
            when (v!!) {
                btnSaveFencesZone -> updateFences(mFencesBean)
                mRightImageButton -> deleteFences(mFencesBean)
            }
        }
    }
}