package com.zxhl.main.page.fragment

import android.os.Handler
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationUtils
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.model.chat.ConstellationEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.NoDoubleClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.CountNumberView
import com.zxhl.main.R
import com.zxhl.main.page.`interface`.ChooseDateCallBack
import com.zxhl.main.page.contract.ConstellationContract
import com.zxhl.main.page.presenter.ConstellationPresenter
import com.zxhl.main.page.view.DialogUtils
import kotlinx.android.synthetic.main.fragment_layout_constellation_match.*
import me.jessyan.autosize.internal.CancelAdapt
import java.util.*
import kotlin.collections.ArrayList


class ConstellationMachFragment : BaseFragment(), ConstellationContract.View, ChooseDateCallBack {
    private var mPresenter: ConstellationPresenter? = null
    private var mListImgView: MutableList<ImageView> = ArrayList()
    private var chosseDate: String? = "2022-04-19"
    private var rotate: RotateAnimation? = null
    override fun layoutID(): Int {
        return R.layout.fragment_layout_constellation_match
    }

    override fun init(view: View?) {
        handler.post(myRunnable)
        mPresenter = ConstellationPresenter(this)
        rotate = RotateAnimation(
            0f,
            359f,
            Animation.RELATIVE_TO_PARENT,
            0.5f,
            Animation.RELATIVE_TO_PARENT,
            0.5f
        )
//        rotate!!.interpolator = LinearInterpolator()
////        rotate.setInterpolator(lin);
//        rotate!!.duration = 1200//设置动画持续周期
//        rotate!!.repeatCount = 2//设置重复次数
//        rotate!!.setStartOffset(10);//执行前的等待时间
//        rotate!!.setAnimationListener(object : AnimationListener {
//            override fun onAnimationStart(animation: Animation?) {
//                //动画开始
//                Log.e("MXL", "onAnimationStart")
//            }
//
//            override fun onAnimationEnd(animation: Animation?) {
//                JumpUtils.ConstellResultJump()
//                Log.e("MXL", "onAnimationEnd")
//            }
//
//            override fun onAnimationRepeat(animation: Animation?) {
//                Log.e("MXL", "onAnimationRepeat")
//            }

 //       })

        mListImgView.add(id_img_shuiping)
        mListImgView.add(id_img_shuangyu)
        mListImgView.add(id_img_mojie)
        mListImgView.add(id_img_juxie)
        mListImgView.add(id_img_shuangzi)
        mListImgView.add(id_img_jinniu)
        mListImgView.add(id_img_shizi)
        mListImgView.add(id_img_chunv)
        mListImgView.add(id_img_tianping)
        mListImgView.add(id_img_tianxie)
        mListImgView.add(id_img_sheshou)
        mListImgView.add(id_img_baiyang)
        id_tv_start_mach?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                id_rl_xingzuo.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.rotate_anim))
               // id_rl_xingzuo.startAnimation(rotate)
                id_rl_xingzuo?.postDelayed({
                    JumpUtils.ConstellResultJump()
                }, 3000)

            }

        })
        id_img_choose?.setOnClickListener {
            DialogUtils.showchooseConsellation(mActivity, this)
        }

    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible) {
            mPresenter?.getConstellationPairing(chosseDate.toString())
        }
    }

    override fun setReslut(result: ConstellationEntity?) {
        if (!result?.list.isNullOrEmpty()) {
            for (i in 0 until (result?.list!!.size)) {
//                if(!TextUtils.isEmpty(result.list!![i].headImage)){
//                    mListImgView[i].visibility=View.VISIBLE
//                }else{
//                    mListImgView[i].visibility=View.GONE
//                }
                if (i < result.list!!.size) {
                    Glide.with(this).load(result.list!![i].headImage)
                        .apply(RequestOptions.bitmapTransform(CircleCrop()))
                        .into(mListImgView[i])
                }

            }
        }
    }

    private val handler = Handler()

    private val myRunnable: Runnable = object : Runnable {
        override fun run() {
            Log.e("MXL", "执行")
            handler.postDelayed(this, 10000)
            id_tv_mach_peoples.showNumberWithAnimation(getNum(), CountNumberView.INTREGEX)
        }
    }

    fun getNum(): Float {
        val random = Random()
        var number = random.nextInt(102) + 402
        Log.e("MXL", "Number:" + number)
        return number.toFloat()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (handler != null) {
            handler.removeCallbacks(myRunnable)
        }
    }

    override fun chooseData(date: String?) {
        Log.e("MXL", "星座选择的日期：" + date)
        chosseDate = date
        if (!TextUtils.isEmpty(chosseDate)) {
            mPresenter?.getConstellationPairing(chosseDate.toString())
            setConsellIcon(id_img_choose, Utils.getXingZuo(chosseDate))
            Log.e("MXL", "星座" + Utils.getXingZuo(chosseDate))
        }

    }

    fun setConsellIcon(img: ImageView, str: String) {
        if (TextUtils.equals(str, "摩羯座")) {
            img.setImageResource(R.drawable.button_mojie)
        } else if (TextUtils.equals(str, "水瓶座")) {
            img.setImageResource(R.drawable.button_shuiping)
        } else if (TextUtils.equals(str, "双鱼座")) {
            img.setImageResource(R.drawable.button_shuangyu)
        } else if (TextUtils.equals(str, "白羊座")) {
            img.setImageResource(R.drawable.button_baiyang)
        } else if (TextUtils.equals(str, "金牛座")) {
            img.setImageResource(R.drawable.button_jinniu)
        } else if (TextUtils.equals(str, "双子座")) {
            img.setImageResource(R.drawable.button_shuangzi)
        } else if (TextUtils.equals(str, "巨蟹座")) {
            img.setImageResource(R.drawable.button_juxie)
        } else if (TextUtils.equals(str, "狮子座")) {
            img.setImageResource(R.drawable.button_shizi)
        } else if (TextUtils.equals(str, "处女座")) {
            img.setImageResource(R.drawable.button_chunv)
        } else if (TextUtils.equals(str, "天秤座")) {
            img.setImageResource(R.drawable.button_tiancheng)
        } else if (TextUtils.equals(str, "天蝎座")) {
            img.setImageResource(R.drawable.button_tianxie)
        } else if (TextUtils.equals(str, "射手座")) {
            img.setImageResource(R.drawable.button_sheshou)
        }
    }


}