using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.Runtime.Filters;
using ElectroShop.Models;
using ElectroShop.Services;
using ElectroShop.DAL.Entities;
using ElectroShop.Resources;
using DotVVM.Framework.Storage;

namespace ElectroShop.ViewModels.ElectronicComponent
{
    public class DetailViewModel : MasterPageViewModel
    {
        private readonly ElectronicComponentService electronicComponentService;
        private readonly CathegoryService cathegoryService;
        public List<string> UserTypes { get; set; }

        public ElectronicComponentModel Model { get; set; } = new ElectronicComponentModel();

        public List<ElectronicComponentCathegoryForComponentManagementModel> ParametersModels { get; set; }

        public List<string> Cathegories { get; set; }

        public string SelectedCathegory { get; set; }
        public string ImageUrl { get; set; }

        private readonly IUploadedFileStorage fileStorage;

        public DetailViewModel(ElectronicComponentService electronicComponentService, CathegoryService cathegoryService,
            IUploadedFileStorage fileStorage)
        {
            this.electronicComponentService = electronicComponentService;
            this.cathegoryService = cathegoryService;
            ParametersModels = new List<ElectronicComponentCathegoryForComponentManagementModel>() { };
            Cathegories = cathegoryService.GetAllCategoriesNames();
            this.fileStorage = fileStorage;
        }

        public override async Task PreRender()
        {
            int id = 0;
            if (int.TryParse(Context.Parameters["Id"].ToString(), out id))
            {
                Model = electronicComponentService.GetById(id);
                ImageUrl = Model.ImgBase64;
                SelectedCathegory = Model.ElectronicComponentCategory.Name;
                OnCathegoryChangedPrivate();
            }
            await base.PreRender();
        }

        private void OnCathegoryChangedPrivate()
        {
            if (SelectedCathegory != null)
            {
                var cathegory = cathegoryService.GetCathegoryByName(SelectedCathegory);
                if (cathegory != null)
                {
                    Model.ElectronicComponentCategoryId = cathegory.Id;
                    List<ElectronicComponentCathegoryForComponentManagementModel> parameterModelsTmp =
                        new List<ElectronicComponentCathegoryForComponentManagementModel>();

                    char[] tmp = new char[1];
                    tmp[0] = ';';
                    var cathegoryParameterNames = cathegory.ParametersNames.Split(tmp);
                    var cathegoryParameterValues = Model.ParametersValues.Split(tmp);
                    if (cathegoryParameterValues == null)
                    {
                        cathegoryParameterValues = new string[0];
                    }
                    int i = 0;
                    foreach (var cathegoryParameterName in cathegoryParameterNames)
                    {
                        parameterModelsTmp.Add(new ElectronicComponentCathegoryForComponentManagementModel()
                        {
                            ParameterName = cathegoryParameterName,
                            ParameterValue = cathegoryParameterValues[i]
                        });
                        i++;
                    }

                    ParametersModels = parameterModelsTmp;
                }
            }
        }

        public void DownloadDatasheet(Guid fileId, string name)
        {
            if (fileId != null)
            {
                using (var stream = fileStorage.GetFile(fileId))
                {
                    Context.ReturnFile(stream, "Datasheet_" + name + ".pdf", "application/pdf");
                }
            }
        }
    }
}
