package com.jld.vod.view

import android.content.Intent
import android.media.Image
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.jld.vod.R
import com.jld.vod.base.BaseActivity
import com.jld.vod.utils.SharedPrefUtils

/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/8/31 15:54
 */
class SeatSettingsActivity : BaseActivity() ,View.OnClickListener {
    private var mContent: TextView? = null
    private var mAdd: Button? = null
    private var mAdd2: Button? = null
    private var mAdd5: Button? = null
    private var mAdd10: Button? = null
    private var closeBtn: ImageView? = null
    private var mSubtract: Button? = null
    private var mSubtract2: Button? = null
    private var mSubtract5: Button? = null
    private var mSubtract10: Button? = null
    private var content = 0
    private var mSubmit: Button? = null
    private val mHandler =
        Handler(Handler.Callback { message ->
            when (message.what) {
                1 -> {
                    //检测字数》100 设置字大小
                   when(seatNumberRange()){
                       1->{
                           //0-99
                           mContent!!.textSize = 240F
                       }
                       10->{
                           //100-999
                           mContent!!.textSize = 200F
                       }
                       100->{
                           //1000+
                           mContent!!.textSize = 140F
                       }
                   }
                    mContent!!.text = content.toString()
                }
            }
            false
        })

    override fun getLayoutId(): Int {
        return R.layout.activity_seat_settings
    }

    /**
     * 初始化数据
     */
    override fun initView() {
        super.initView()
        mContent = findViewById(R.id.tv_setting_content)
        mSubtract = findViewById(R.id.bt_setting_subtract1)
        mSubtract2 = findViewById(R.id.bt_setting_subtract2)
        mSubtract5 = findViewById(R.id.bt_setting_subtract5)
        mSubtract10 = findViewById(R.id.bt_setting_subtract10)

        mAdd = findViewById(R.id.bt_setting_add1)
        mAdd2 = findViewById(R.id.bt_setting_add2)
        mAdd5 = findViewById(R.id.bt_setting_add5)
        mAdd10 = findViewById(R.id.bt_setting_add10)

        mSubmit = findViewById(R.id.bt_setting_submit)
        closeBtn = findViewById(R.id.r_button_offDebug)
        mSubmit?.text = getString(R.string.define)
        val string = SharedPrefUtils.getString(this, "seatid")
        Log.d("","SeatSettingsActivityinitView: $string")
        content = if (string.isEmpty()){
            mContent!!.text.toString().toInt()
        }else{
            string.toInt()
        }
        sendMessage()
        mSubtract?.setOnClickListener(this)
        mSubtract2?.setOnClickListener(this)
        mSubtract5?.setOnClickListener(this)
        mSubtract10?.setOnClickListener(this)
        mAdd?.setOnClickListener(this)
        mAdd2?.setOnClickListener(this)
        mAdd5?.setOnClickListener(this)
        mAdd10?.setOnClickListener(this)
        closeBtn?.setOnClickListener (this)
        mSubmit?.setOnClickListener(this)
    }

    /**
     * 判断数值是否小于
     */
    private fun testNum(){
        if (content<1){
            content = 0
        }
    }

    /**
     * 异步修改
     */
    private fun sendMessage(){
        val msg = Message()
        msg.what = 1
        mHandler.sendMessage(msg)
    }

    /**
     * 获取当前座位号范围
     */
    private fun seatNumberRange():Int{
        if (content<100) return 1
        if (content in 100..999) return 10
        return 100
    }

    /**
     * 设置点击事件
     */
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.bt_setting_subtract1->{
                if (content > 1) {
                    content--
                    sendMessage()
                }
            }
            R.id.bt_setting_subtract2->{
                if (content > 1) {
                    content -= 2
                    testNum()
                    sendMessage()
                }
            }
            R.id.bt_setting_subtract5->{
                if (content > 1) {
                    content-=5
                    testNum()
                    sendMessage()
                }
            }
            R.id.bt_setting_subtract10->{
                if (content > 1) {
                    content-=10
                    testNum()
                    sendMessage()
                }
            }
            R.id.bt_setting_add1->{
                content++
                sendMessage()
            }
            R.id.bt_setting_add2->{
                content+=2
                sendMessage()
            }
            R.id.bt_setting_add5->{
                content+=5
                sendMessage()
            }
            R.id.bt_setting_add10->{
                content+=10
                sendMessage()
            }
            R.id.r_button_offDebug->{
                //关闭并返回设置界面
                val intent = Intent(this,SettingActivity::class.java)
                startActivity(intent)
            }
            R.id.bt_setting_submit->{
                //存下座位号
                SharedPrefUtils.putString(this,"seatid",content.toString() + "")
                val intent = Intent(this,SplashActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
    }
}