/*
 * Copyright (c) 2016, Aegir Labs LLC. All rights reserved.
 */

package com.aegir.demo.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aegir.demo.R;
import com.aegir.demo.model.FloorItem;

import java.util.Locale;

/**
 * View used in FloorItemAdapter. Displays a Floor.
 */
public class FloorItemView extends RelativeLayout {

    public interface OnInfoImageViewClickListener {
        void onClick(FloorItem floorItem);
    }

    // region Fields

    private TextView descriptionTextView;
    private LinearLayout wayfindingNotAvailableContainer;
    private ImageView infoImageView;

    // endregion

    // region Constructors

    /**
     * @param c the activity context
     */
    public FloorItemView(Context c) {
        this(c, null);
    }

    /**
     * @param context the activity context
     * @param attrs   layout attributes
     */
    public FloorItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * @param context  the activity context
     * @param attrs    layout attributes
     * @param defStyle style resource
     */
    public FloorItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.floor_item_view_children, this, true);
        setupChildren();
    }

    // endregion

    // region Public Methods

    /**
     * @param parent the parent to use for inflation
     * @return new view instance
     */
    public static FloorItemView inflate(ViewGroup parent) {
        FloorItemView floorItemView = (FloorItemView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.floor_item_view, parent, false);
        return floorItemView;
    }

    /**
     * @param floor the floor object for this floor item view
     */
    public void setItem(FloorItem floor, OnInfoImageViewClickListener clickListener) {
        descriptionTextView.setText(floor.getName());
        wayfindingNotAvailableContainer.setVisibility(floor.isWayfindingAvailable() ? View.GONE : View.VISIBLE);
        infoImageView.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onClick(floor);
            }
        });
    }

    // endregion

    // region Private Methods

    /**
     *
     */
    private void setupChildren() {
        descriptionTextView = findViewById(R.id.tv_floor_description_item);
        wayfindingNotAvailableContainer = findViewById(R.id.ll_wayfinding_available_container_item);
        infoImageView = findViewById(R.id.info_image_view);
    }

    // endregion
}
