using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;
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 CouponHistoryViewModel : BaseViewModel
    {
        #region Fields
        private string loadingText = "Loading...";
        private bool isLoading = true;
        #endregion

        #region Properties
        public string LoadingText
        {
            get => loadingText;
            set
            {
                SetProperty(ref loadingText, value);
            }
        }
        public bool IsLoading
        {
            get => isLoading;
            set
            {
                SetProperty(ref isLoading, value);
            }
        }
        public ObservableCollection<CouponHistoryDto> CouponsHistory { get; }
        #endregion

        #region Constructor
        public CouponHistoryViewModel()
        {
            Title = "Coupon History";
            CouponsHistory = new ObservableCollection<CouponHistoryDto>();
            GetCouponsHistory();
        }
        #endregion

        #region Methods 
        private async void GetCouponsHistory()
        {
            string appToken = Preferences.Get(SecureStorageData.Token, "");
            GMCabsDriverService gMCabsDriverService = new GMCabsDriverService();
            CouponsHistory.Clear();
            var coupons = await gMCabsDriverService.GetCouponsHistory(appToken);
            if (coupons.Count > 0)
            {
                IsLoading = false; LoadingText = "";
            }
            else
            {
                IsLoading = true; LoadingText = "No coupons history available";
            }
            foreach (CouponHistoryDto coupon in coupons)
            {
                coupon.isRedemed = false;
                if (coupon.RedeemedDate.HasValue)
                {
                    coupon.CouponText = "Redeemed";
                    coupon.CouponBackgroundColor = "#2CC84E";
                    coupon.isRedemed = true;
                }
                else
                {
                    coupon.CouponText = "EXPIRED";
                    coupon.CouponBackgroundColor = "#C82C2C";
                }
                CouponsHistory.Add(coupon);
            }
        }
        #endregion
    }
}
