package com.wdkl.app.ncs.callingbed.adapter

import android.content.Context
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.TextView
import com.wdkl.app.ncs.callingbed.R
import com.wdkl.ncs.android.middleware.model.vo.CustomerFeeConfigByGroupNameVO
import com.wdkl.ncs.android.middleware.model.vo.CustomerFeeConfigVO
import com.wdkl.ncs.android.middleware.utils.StringUtil

class CostItemAdapter : RecyclerView.Adapter<CostItemAdapter.ParentViewHolder> {

    private var context: Context
    private var mainData: ArrayList<CustomerFeeConfigByGroupNameVO>

    constructor(context: Context, data: ArrayList<CustomerFeeConfigByGroupNameVO>) {
        this.context = context
        this.mainData = data
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ParentViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_cost_main_view, parent, false)
        val viewHolder = ParentViewHolder(view)

        return viewHolder
    }

    override fun onBindViewHolder(holder: ParentViewHolder?, position: Int) {
        holder?.costGroupName?.text = mainData.get(position).feeGroupName
        holder?.costGroupTotal?.text = StringUtil.getResString(R.string.subtotal) + mainData.get(position).subtotal

        val layoutManager = LinearLayoutManager(context)
        holder?.costDetail?.layoutManager = layoutManager
        holder?.costDetail?.adapter = CostDetailAdapter(mainData.get(position).customerFeeConfigList)
    }

    override fun getItemCount(): Int {
        return mainData.size
    }

    class ParentViewHolder : RecyclerView.ViewHolder {
        var costGroupName : TextView
        var costGroupTotal : TextView
        var costDetail : RecyclerView

        constructor(itemView: View): super(itemView) {
            costGroupName = itemView.findViewById(R.id.tv_cost_group_name)
            costGroupTotal = itemView.findViewById(R.id.tv_cost_group_total)
            costDetail = itemView.findViewById(R.id.rv_cost_detail)
        }
    }

    class CostDetailAdapter : RecyclerView.Adapter<CostDetailAdapter.ChildViewHolder> {
        private var costData: List<CustomerFeeConfigVO>

        constructor(data: List<CustomerFeeConfigVO>) {
            costData = data
        }

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ChildViewHolder {
            val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_cost_detail, parent, false)
            val viewHolder = ChildViewHolder(view)

            return viewHolder
        }

        override fun onBindViewHolder(holder: ChildViewHolder?, position: Int) {
            holder?.costName?.text = costData.get(position).feeName
            holder?.costValue?.text = "" + costData.get(position).feeValue
            holder?.costId?.text = costData.get(position).keyCode
            holder?.costUnit?.text = costData.get(position).unit
        }

        override fun getItemCount(): Int {
            return costData.size
        }

        class ChildViewHolder : RecyclerView.ViewHolder {
            var costName : TextView
            var costValue : TextView
            var costId : TextView
            var costUnit : TextView

            constructor(item : View) : super(item) {
                costName = item.findViewById(R.id.tv_item_cost_name)
                costValue = item.findViewById(R.id.tv_item_cost_value)
                costId = item.findViewById(R.id.tv_item_cost_id)
                costUnit = item.findViewById(R.id.tv_item_cost_unit)
            }
        }
    }
}