package com.yidianling.medical.archives

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.view.*
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.utils.StatusBarUtils
import com.ydl.ydlcommon.view.dialog.NormalDialog
import com.yidianling.common.tools.ToastUtil
import com.yidianling.im.R
import com.yidianling.medical.utils.EditTextUtils
import com.yidianling.uikit.custom.http.ServiceImpl
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.medical_add_new_archives_activity.*


/**
 * 新增问诊人
 * */
class MedicalAddNewArchivesActivity : BaseActivity() {


    companion object {
        var mMarried = -1
        var mRelation = -1
        //之前输入内容长度
        var oldLength = 0;
        private val AUTOSIZE = 1027;

        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MedicalAddNewArchivesActivity::class.java)
            context.startActivity(starter)
        }
    }
    private  val mHandler:Handler = object :Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (msg?.what == AUTOSIZE){

                val msp = SpannableString(et_idcard.text.toString())
                msp.setSpan(
                    AbsoluteSizeSpan(16, true),
                    0,
                    et_idcard.text.length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                et_idcard.setText(msp)
                et_idcard.setSelection(et_idcard.text.length)
            }
        }
    }

    override fun layoutResId(): Int {
        return R.layout.medical_add_new_archives_activity
    }

    override fun initDataAndEvent() {
        mMarried = -1
        mRelation = -1
        initStatus()


        group_merried.setOnCheckedChangeListener { group, checkedId ->
            //婚姻状态
            when (checkedId) {
                R.id.married -> {
                    //已婚  1
                    mMarried = 1

                }
                R.id.unmarried -> {
                    //未婚  0
                    mMarried = 0
                }

                R.id.widowed -> {
                    //丧偶  2
                    mMarried = 2
                }
                R.id.divorce -> {
                    //离异  3
                    mMarried = 3
                }
                else -> {

                    mMarried = -1
                }
            }
        }
        group_relation.setOnCheckedChangeListener { group, checkedId ->
            //与本人关系
            when (checkedId) {
                R.id.relation_me -> {
                    //本人  0
                    mRelation = 1

                }
                R.id.relation_parent -> {
                    //父母  0
                    mRelation = 1
                }

                R.id.relation_spouse -> {
                    //配偶  2
                    mRelation = 2
                }
                R.id.relation_children -> {
                    //子女  3
                    mRelation = 3
                }
                R.id.relation_other -> {
                    //其他  4
                    mRelation = 4
                }
                else -> {

                    mRelation = -1
                }
            }
        }


        add_archives_save.setOnClickListener {
            //保存新增问诊人
            checkInfo()
        }
        EditTextUtils.disableCopyAndPaste(et_name)
        //徐总的需求暂时不上了，哇徐总优秀
//        et_idcard.addTextChangedListener(object :TextWatcher{
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//
//
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//            }
//
//            override fun afterTextChanged(s: Editable?) {
//                var sLength = s.toString().length;
//                if(sLength > oldLength){
//                    val msp = SpannableString(s.toString())
//                    msp.setSpan(
//                        AbsoluteSizeSpan(22, true),
//                        Math.max(s.toString().length - 1,0),
//                        s.toString().length,
//                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//                    )
//                    oldLength = sLength;
//                    et_idcard.setText(msp)
//                    et_idcard.setSelection(sLength)
//                    var message = Message()
//                    message.what = AUTOSIZE;
//                    mHandler.sendMessageDelayed(message,3000)
//                }else{
//                    oldLength = sLength
//                }
//            }
//
//        })

        iv_back.setOnClickListener { finish() }

        showDialogTip()
    }

    private fun showDialogTip() {
        val builder = NormalDialog.Builder(this@MedicalAddNewArchivesActivity, "center", false)
        builder.setTitle("")
        var strs = arrayOf("就诊人的身份证号码", "就诊人的授权同意", "您需要在提供前征得该儿童监护人的同意")
        builder.setMessageAndBolds(
            "\n为向您添加的就诊人提供问诊服务，您需要向我们提供该就诊人的身份证号码，并确保在提供前已取得该就诊人的授权同意；若该就诊人为儿童的，您需要在提供前征得该儿童监护人的同意。\n",
            strs
        )
        builder.setPositiveButton(
            "同意"
        ) { dialog, which ->
            dialog.dismiss()
        }.setNegativeButton(
            "不同意"
        ) { dialog, which ->
            //关闭页面
            finish()
            dialog.dismiss()
        }
        builder.setRight_color("#3464EC")
        builder.setLeft_color("#8595A9")
        builder.setContent_color("#10233A")

        builder.create().show()

    }

    @SuppressLint("CheckResult")
    private fun checkInfo() {
        if (et_name.text.isNullOrEmpty()) {
            ToastUtil.toastShort("请填写姓名")
            return
        }
        if (et_idcard.text.isNullOrEmpty()) {
            ToastUtil.toastShort("请填写身份证号")
            return
        }

        if (et_idcard.text.length < 15 || et_idcard.text.length > 18) {
            ToastUtil.toastShort("请填写有效身份证号")
            return
        }
        if (mMarried == -1) {
            ToastUtil.toastShort("请选择婚姻状况")
            return
        }
        if (mRelation == -1) {
            ToastUtil.toastShort("请选择与就诊人的关系")
            return
        }

        ServiceImpl.instance.createArchives(
            et_name.text.toString(),
            et_idcard.text.toString(),
            mMarried,
            mRelation
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if ("200" == it.code) {
                    finish()
                    ToastUtil.toastShort("保存成功")
                } else {
                    ToastUtil.toastShort(it.msg)
                }
            }, {
                it.printStackTrace()
                ToastUtil.toastShort("网络错误")
            })


    }

    private fun initStatus() {
        StatusBarUtils.setTransparentForImageView(this@MedicalAddNewArchivesActivity, null)
        StatusBarUtils.statusBarLightMode(this@MedicalAddNewArchivesActivity)
    }
}