﻿using System;
using Android.Views;
using Android.Widget;
using ExpertSolutions.Speurtochten.App.Android.Common;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.App;
using Android.Graphics;
using ExpertSolutions.Speurtochten.App.Android.Common.Entities;

namespace ExpertSolutions.Speurtochten.App.Android
{
    public class TargetLocationListAdapter : BaseAdapter<Routepunt>
    {
        private readonly List<Routepunt> _targetLocations;

        public TargetLocationListAdapter(List<Routepunt> targetLocations)
        {
            _targetLocations = targetLocations;
        }

        public override Routepunt this[int position] => _targetLocations[position];

        public override int Count => _targetLocations.Count;

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;

            if (view == null)
            {
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Locations_list, parent, false);

                var imageView = view.FindViewById<ImageView>(Resource.Id.locationImage);
                var locationDescriptionView = view.FindViewById<TextView>(Resource.Id.locationDescription);

                view.Tag = new ViewHolder
                {
                    Image = imageView,
                    Description = locationDescriptionView
                };
            }

            var holder = (ViewHolder)view.Tag;

            var bytes = Convert.FromBase64String(_targetLocations[position].File.Base64StringRempoveHeaderData());
            var image = FromArray(bytes) as Bitmap;

            var viewImage =  new ImageView(Application.Context);
            viewImage.SetImageBitmap(image);
            
            holder.Description.Text = _targetLocations[position].Description;

            return view;
        }
    }
}