package com.etong.smart.Main

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import com.etong.smart.Other.RxServie
import com.etong.smart.Other.toDate
import com.etong.smart.R
import kotlinx.android.synthetic.main.fragment_to_do_list.*
import rx.Subscription
import java.util.*


class ToDoListFragment : Fragment() {
    data class Data(val type: Int, val data: Any? = null)
    companion object {
        val TYPE_TITLE_PROCESS = 0
        val TYPE_TITLE_DONE = 1
        val TYPE_PROCESS = 2
        val TYPE_DONE = 3
        val TYPE_NOT_START = 4
    }

    private val dataList = arrayListOf<Data>()
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater!!.inflate(R.layout.fragment_to_do_list, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        recyclerView.adapter = Adapter(dataList)
        recyclerView.layoutManager = LinearLayoutManager(context)
        refreshLayout.setOnRefreshListener {
            loadWebData()
        }
        loadWebData()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onStop() {
        mObserver?.unsubscribe()
        refreshLayout.isRefreshing = false
        super.onStop()
    }

    private var mObserver: Subscription? = null
    private fun loadWebData() {
        refreshLayout.isRefreshing = true
        mObserver = RxServie.getWorkOrder {
            if (it != null) {
                dataList.clear()
                for (i in it) {
                    when (it.indexOf(i)) {
                        0 -> {
                            i.mapTo(dataList) { Data(type = TYPE_NOT_START, data = it) }
                        }
                        1 -> {
                            if (i.isNotEmpty()) {
                                dataList.add(Data(TYPE_TITLE_PROCESS))
                                i.mapTo(dataList) { Data(type = TYPE_PROCESS, data = it) }
                            }
                        }
                        2 -> {
                            if (i.isNotEmpty()) {
                                dataList.add(Data(TYPE_TITLE_DONE))
                                i.mapTo(dataList) { Data(type = TYPE_DONE, data = it) }
                            }
                        }
                    }
                }
            }
            endLoading()
        }
    }

    private fun endLoading() {
        if (refreshLayout != null) {
            refreshLayout.isRefreshing = false
            recyclerView.adapter.notifyDataSetChanged()
            checkView()
        }
    }

    private fun checkView() {
        if (dataList.isEmpty()) {
            recyclerView.visibility = View.GONE
            nullView.visibility = View.VISIBLE
        } else {
            recyclerView.visibility = View.VISIBLE
            nullView.visibility = View.GONE
        }
    }


    inner private class Adapter(val data: ArrayList<Data>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


        override fun getItemCount() = data.count()

        override fun getItemViewType(position: Int) = data[position].type

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
            when (holder) {
                is ProcessViewHolder -> {
                    holder.bindView(data[position].data)
                }
                is DoneViewHolder -> {
                    holder.bindView(data[position].data)
                }
                is NotStartViewHolder -> {
                    holder.bindView(data[position].data)
                }
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
            val layoutInflater = LayoutInflater.from(parent?.context)
            when (viewType) {
                TYPE_TITLE_DONE -> return TitleViewHolder(layoutInflater.inflate(R.layout.item_todo_done_title, parent, false))
                TYPE_TITLE_PROCESS -> return TitleViewHolder(layoutInflater.inflate(R.layout.item_todo_process_title, parent, false))
                TYPE_DONE -> return DoneViewHolder(layoutInflater.inflate(R.layout.item_todo_done, parent, false))
                TYPE_PROCESS -> return ProcessViewHolder(layoutInflater.inflate(R.layout.item_todo_process, parent, false))
                else -> return NotStartViewHolder(layoutInflater.inflate(R.layout.item_todo_not_start, parent, false))
            }
        }

        inner private class TitleViewHolder(item: View) : RecyclerView.ViewHolder(item)
        inner private class ProcessViewHolder(item: View) : RecyclerView.ViewHolder(item) {
            val tv_title: TextView
            val tv_content: TextView
            val tv_time: TextView
            val checkBox: CheckBox

            init {
                checkBox = item.findViewById(R.id.checkbox) as CheckBox
                tv_title = item.findViewById(R.id.tv_title) as TextView
                tv_content = item.findViewById(R.id.tv_content) as TextView
                tv_time = item.findViewById(R.id.tv_time) as TextView
            }

            fun bindView(data: Any?) {
                checkBox.isChecked = false
                val d = data as RxServie.WorkOrderBean
                tv_title.text = d.title
                tv_content.text = d.content
                tv_time.text = d.create_date?.toDate()
                itemView.setOnClickListener {
                    toDetail(d)
                }
                checkBox.setOnCheckedChangeListener { compoundButton, b ->
                    RxServie.setStatus(d) {
                        if (it) {
                            loadWebData()
                        }
                    }
                }
            }
        }

        inner private class DoneViewHolder(item: View) : RecyclerView.ViewHolder(item) {
            val tv_title: TextView
            val tv_content: TextView
            val tv_time: TextView
            val checkBox: CheckBox

            init {
                checkBox = item.findViewById(R.id.checkbox) as CheckBox
                tv_title = item.findViewById(R.id.tv_title) as TextView
                tv_content = item.findViewById(R.id.tv_content) as TextView
                tv_time = item.findViewById(R.id.tv_time) as TextView
            }

            fun bindView(data: Any?) {
                checkBox.isChecked = true
                val d = data as RxServie.WorkOrderBean
                tv_title.text = d.title
                tv_content.text = d.content
                tv_time.text = d.create_date?.toDate()
                itemView.setOnClickListener {
                   toDetail(d)
                }
                checkBox.setOnCheckedChangeListener { compoundButton, b ->
                    RxServie.setStatus(d) {
                        if (it) {
                            loadWebData()
                        }
                    }
                }
            }
        }


        inner private class NotStartViewHolder(item: View) : RecyclerView.ViewHolder(item) {
            val tv_title: TextView
            val tv_content: TextView
            val tv_time: TextView

            init {
                tv_title = item.findViewById(R.id.tv_title) as TextView
                tv_content = item.findViewById(R.id.tv_content) as TextView
                tv_time = item.findViewById(R.id.tv_time) as TextView
            }

            fun bindView(data: Any?) {
                val d = data as RxServie.WorkOrderBean
                tv_title.text = d.title
                tv_content.text = d.content
                tv_time.text = d.create_date?.toDate()
                itemView.setOnClickListener {
                    toDetail(d)
                }
            }
        }


        private fun toDetail(data:RxServie.WorkOrderBean){
            val intent = Intent(activity, ToDoListDetailActivity::class.java)
            intent.putExtra("id", data.id)
            intent.putExtra("roomName",data.title)
            startActivity(intent)
        }

    }


}
