package com.bc_chat.bc_base.view

import android.os.Bundle
import android.support.design.widget.BottomSheetDialogFragment
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import com.bc_chat.bc_base.R
import com.bc_chat.bc_base.databinding.DialogRegionBinding
import com.bc_chat.bc_base.utils.PickerUtils
import com.zhaohaoting.framework.abs.AbsDialogFragment
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.dto.ProvinceDTO
import com.zhaohaoting.framework.view.picker.framework.entity.City
import com.zhaohaoting.framework.view.picker.framework.entity.County
import com.zhaohaoting.framework.view.picker.framework.entity.Province
import com.zhaohaoting.framework.view.picker.framework.picker.AddressPicker

/**
 * author：Created by zhaohaoting on 2019/2/22
 * email：526309416@qq.com
 * desc：位置对话框
 */
class AddressDialogFragment : AbsDialogFragment<BaseContract.BasePresenter>() {

    private var binding: DialogRegionBinding? = null
    private var addressPickListener: AddressPicker.OnAddressPickListener? = null
    override fun initialize() {
        binding = rootDataBinding as DialogRegionBinding
        val createAddressPickerWindow = PickerUtils.createAddressPickerWindow(getActivity()!!, ProvinceDTO.getProvinces())
        binding?.flContainer?.addView(createAddressPickerWindow.contentView)
        binding?.tvCancel?.setOnClickListener {
            dismissAllowingStateLoss()
        }
        binding?.tvConfirm?.setOnClickListener {
            dismissAllowingStateLoss()
            addressPickListener?.onAddressPicked(createAddressPickerWindow.selectedProvince, createAddressPickerWindow.selectedCity, createAddressPickerWindow.selectedCounty)
        }
    }

    override fun onStart() {
        super.onStart()
        var params: WindowManager.LayoutParams = dialog.window.attributes
        params.gravity = Gravity.BOTTOM
        dialog.window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

    fun setOnAddressPickListener(onAddressPicked: (province: Province, city: City, county: County) -> Unit) {
        addressPickListener = AddressPicker.OnAddressPickListener { province, city, county -> onAddressPicked(province, city, county) }
    }

    override fun initPresenter(): BaseContract.BasePresenter? {
        return null
    }

    override fun getLayoutResId(): Int {
        return R.layout.dialog_region
    }
}
