package com.zswc.creativity.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
import com.zswc.creativity.R
import com.zswc.creativity.base.MBFragment
import com.zswc.creativity.databinding.FragmentAccountAnalysisBinding
import com.zswc.creativity.model.IconData
import com.zswc.creativity.mp.LineChartManager
import com.zswc.creativity.mp.MyFofChartBean
import com.zswc.creativity.mp.MyMarkerView
import com.zswc.creativity.vmodel.AccountAnalysisVModel
import java.util.*

class AccountAnalysisFragment : MBFragment<AccountAnalysisVModel, FragmentAccountAnalysisBinding>() {


    private val mXData = ArrayList<String>()
    private var mMarkerView: MyMarkerView? = null

    private var mChartManager: LineChartManager? = null


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.getInfo()
        // 初始化表格manager
        mChartManager = LineChartManager(binding.chart)
        mMarkerView = MyMarkerView(context, R.layout.chart_marker_view)
        mChartManager!!.setMarkView(mMarkerView)

        vm.info.observe(this, androidx.lifecycle.Observer {

            initData(it)
        })



    }

   private fun  initData(bean: IconData){//给折线图数据
       val list: ArrayList<MyFofChartBean> = ArrayList<MyFofChartBean>()

       for((index,item)in bean!!.x.withIndex()){
           val item = MyFofChartBean(item, bean.y[index].toFloat())
           list.add(item)
       }
       binding.chart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
           private var mHighlight: Highlight? = null
           override fun onValueSelected(entry: Entry, highlight: Highlight) {
               mHighlight = highlight
           }

           override fun onNothingSelected() {
               if (mHighlight == null) {
                   mHighlight = Highlight(mMarkerView!!.highlightXValue, mMarkerView!!.highlightYValue, 1)
               }
               mMarkerView!!.showMarkView(mHighlight!!.x, mHighlight!!.y)
           }
       })
       setLineChartData(list)

   }


    private fun setLineChartData(list: List<MyFofChartBean>?) {
        if (list != null && !list.isEmpty()) {
            mXData.clear()
            val size = list.size
            val entries: MutableList<Entry> = ArrayList()
            for (i in 0 until size) {
                val dataBean = list[i]
                if (dataBean != null) {
                    if (!java.lang.Float.isNaN(dataBean.networth)) { //getResources().getDrawable(R.drawable.icon_chart_dot)
                        val e = Entry(i.toFloat(), dataBean.networth, null)
                        entries.add(e)
                    }
                    mXData.add(dataBean.inputDate)
                }
            }
            val xAxis: XAxis = binding.chart.xAxis
            //            xAxis.setLabelCount(list.size());
            xAxis.setLabelCount(3, true)
            //            xAxis.setAxisMinimum(0f);
//            xAxis.setAxisMaximum((float) list.size());
            xAxis.valueFormatter = object : IndexAxisValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    val i = value.toInt()
                    Log.e("-shy-", "i=$i")
                    Log.e("-shy-", "mXData.size=" + mXData.size)
                    return try {
                        mXData[i]
                    } catch (e: Exception) {
                        ""
                    }

                }
            }
            // 设置lineChart的数据
            mChartManager!!.setData(entries)
            // 给y轴设置最大值
            val yMax: Float = binding.chart.yMax
            binding.chart.axisLeft.axisMaximum = yMax + 0.1f

            // 设置X轴的label的集合
            mMarkerView!!.setXAxisLabels(mXData)
            // 指定最后一个点为高亮，让其自定显示MarkView
            val entry = entries[size - 1]
            mMarkerView!!.showMarkView(entry.x, entry.y)
            binding.chart.animateXY(800, 800)
            binding.chart.invalidate()
        }else{//没有数据
            if(mXData.size>0){//清空数据
                binding.chart.clear()
            }


        }

    }


    override fun vmClass(): Class<AccountAnalysisVModel> {
        return AccountAnalysisVModel::class.java
    }
    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentAccountAnalysisBinding {
        return FragmentAccountAnalysisBinding.inflate(inflater, container, false)
    }


    override fun title(): String? {
        return null
    }

}
