package com.casic.br.view.device.waterheater

import android.app.Activity
import android.content.Intent
import android.widget.TimePicker
import com.casic.br.R
import com.github.gzuliyujiang.wheelpicker.NumberPicker
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.convertColor
import com.pengxh.kt.lite.extensions.sp2px
import com.pengxh.kt.lite.utils.ImmerseStatusBarUtil
import kotlinx.android.synthetic.main.activity_add_time_config.*
import kotlinx.android.synthetic.main.include_text_title.*
import java.util.*


class AddTimeConfigActivity : KotlinBaseActivity() {

    private val kTag = "AddTimeConfigActivity"
    private var selectedTime = ""
    private var selectedTemp = ""

    override fun initLayoutView(): Int = R.layout.activity_add_time_config

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        ImmerseStatusBarUtil.setColor(this, R.color.white.convertColor(this))

        leftTagView.setOnClickListener { finish() }
        titleView.text = "添加预约"
    }

    override fun initData() {

    }

    override fun initEvent() {
        //设置点击事件不弹键盘
        startTimePicker.descendantFocusability = TimePicker.FOCUS_BLOCK_DESCENDANTS
        startTimePicker.setIs24HourView(true)
        val now = Calendar.getInstance()
        startTimePicker.hour = now.get(Calendar.HOUR_OF_DAY)
        startTimePicker.minute = now.get(Calendar.MINUTE)
        selectedTime = "${startTimePicker.hour}:${startTimePicker.minute}"
        startTimePicker.setOnTimeChangedListener { _, hourOfDay, minute ->
            selectedTime = "$hourOfDay:$minute"
        }

        selectedTemp = "36${resources.getString(R.string.temperature_unit)}"
        tempView.text = selectedTemp
        setTempLayout.setOnClickListener {
            val picker = NumberPicker(this)
            picker.wheelView.textSize = 12f.sp2px(this).toFloat()
            picker.wheelView.selectedTextSize = 14f.sp2px(this).toFloat()
            picker.setOnNumberPickedListener { _, item ->
                selectedTemp = item.toString()
                tempView.text = selectedTemp
            }
            picker.wheelLayout.setOnNumberSelectedListener { position, _ ->
                picker.titleView.text = picker.wheelView.formatItem(position)
            }
            picker.setFormatter { item ->
                "$item${resources.getString(R.string.temperature_unit)}"
            }
            picker.setRange(36, 60, 1)
            picker.setDefaultValue(36)
            picker.titleView.text = "水温"
            picker.show()
        }

        rightTagView.setOnClickListener {
            val intent = Intent()
            intent.putExtra("selectedTime", selectedTime)
            intent.putExtra("selectedTemp", selectedTemp)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }
}