/*
 * Copyright (c) 2021 PSMForums. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.joshuacerdenia.android.nicefeed.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.joshuacerdenia.android.nicefeed.R
import com.joshuacerdenia.android.nicefeed.data.model.TopicBlock

class TopicAdapter(
    private val context: Context,
    private val listener: OnItemClickListener,
) : ListAdapter<TopicBlock, TopicAdapter.TopicHolder>(DiffCallback()) {

    var numOfItems = 0 // Initial value only

    interface OnItemClickListener {
        fun onTopicSelected(topic: String)
    }

    override fun submitList(list: MutableList<TopicBlock>?) {
        super.submitList(list?.take(numOfItems))
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopicHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.grid_item_topic, parent, false)
        return TopicHolder(view)
    }

    override fun onBindViewHolder(holder: TopicHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class TopicHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {

        private lateinit var topicBlock: TopicBlock
        private val topicTextView: TextView = itemView.findViewById(R.id.topic_text_view)

        init {
            itemView.setOnClickListener(this)
        }

        fun bind(topicBlock: TopicBlock) {
            this.topicBlock = topicBlock
            topicTextView.text = context.getString(R.string.hashtag, topicBlock.topic)
            val color = ContextCompat.getColor(context, topicBlock.color)
            itemView.setBackgroundColor(color)
        }

        override fun onClick(v: View) {
            listener.onTopicSelected(topicTextView.text.toString())
        }
    }

    private class DiffCallback : DiffUtil.ItemCallback<TopicBlock>() {

        override fun areItemsTheSame(oldItem: TopicBlock, newItem: TopicBlock): Boolean {
            return oldItem.topic == newItem.topic
        }

        override fun areContentsTheSame(oldItem: TopicBlock, newItem: TopicBlock): Boolean {
            return oldItem == newItem
        }
    }
}