using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GMCabsDriverAssistantSolution.ViewModels
{
    public class CouponsViewModel : BaseViewModel
    {
        #region Fields
        private string emptyViewMessage;
        #endregion

        #region Properties


        public string EmptyViewMessage
        {
            get => emptyViewMessage;
            set => SetProperty(ref emptyViewMessage, value);
        }

        public ObservableCollection<CouponDto> Coupons { get; }
        public Command SyncCouponCommand { get; }
        #endregion

        #region Constructor
        [Obsolete]
        public CouponsViewModel()
        {
            Title = "Coupons";
            SyncCouponCommand = new Command(async () => await GetCoupons());
            Coupons = new ObservableCollection<CouponDto>();
            HandleReceivedMessages();
        }
        #endregion

        #region Methods
        public void OnAppearing()
        {
            Task.Run(async () => await GetCoupons());
        }
        private async Task GetCoupons()
        {
            EmptyViewMessage = "Loading Coupons...";
            string appToken = Preferences.Get(SecureStorageData.Token, "");
            GMCabsDriverService gMCabsDriverService = new GMCabsDriverService();

            Coupons.Clear();
            List<CouponDto> coupons = await gMCabsDriverService.GetCoupons(appToken);
            if (coupons != null && coupons.Count > 0)
            {
                foreach (CouponDto coupon in coupons)
                {
                    QRCodeGenerator qrGenerator = new QRCodeGenerator();
                    QRCodeData qrCodeData = qrGenerator.CreateQrCode(coupon.Id.ToString(), QRCodeGenerator.ECCLevel.Q);
                    BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);
                    byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20);
                    coupon.Image = ImageSource.FromStream(() => new MemoryStream(qrCodeAsBitmapByteArr));

                    Coupons.Add(coupon);
                }
            }
            else
            {
                EmptyViewMessage = "No Coupons available";
            }
        }

        [Obsolete]
        void HandleReceivedMessages()
        {
            MessagingCenter.Unsubscribe<App, CouponDto>(this, nameof(App));
            MessagingCenter.Subscribe<App, CouponDto>(this, nameof(App), (sender, couponDto) => {
                Device.BeginInvokeOnMainThread(() => {
                    CouponDto redeemedcoupon = new CouponDto
                    {
                        Id = couponDto.Id,
                        Status = couponDto.Status
                    };
                    List<CouponDto> copyCoupons = new List<CouponDto>(Coupons);
                    Coupons.Clear();
                    foreach (var item in copyCoupons)
                    {
                        if (item.Id == redeemedcoupon.Id)
                        {
                            item.Image = ImageSource.FromFile("scanned.png");
                        }
                        Coupons.Add(item);
                    }



                });


            });

        }
        #endregion
    }
}
