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.BusinessPack.Controls;

namespace ElectroShop.ViewModels.ElectronicComponent
{
    [Authorize(Roles = "Warehouseman")]
    public class EditViewModel : MasterPageViewModel
    {
        private readonly ElectronicComponentService electronicComponentService;
        private readonly CathegoryService cathegoryService;
        public List<string> UserTypes { get; set; }

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

        [Bind(Direction.Both)]
        public List<ElectronicComponentCathegoryForComponentManagementModel> ParametersModels { get; set; }

        [Bind(Direction.Both)]
        public List<string> Cathegories { get; set; }

        [Bind(Direction.Both)]
        public string SelectedCathegory { get; set; }

        public UploadData UploadImage { get; set; } = new UploadData();

        public UploadData UploadedDataSheet { get; set; } = new UploadData();

        public string ImageUrl { get; set; }

        public string DatasheetUrl { get; set; }

        public EditViewModel(ElectronicComponentService electronicComponentService, CathegoryService cathegoryService)
        {
            this.electronicComponentService = electronicComponentService;
            this.cathegoryService = cathegoryService;
            Cathegories = cathegoryService.GetAllCategoriesNames();
        }

        public override Task Init()
        {
            int id = 0;
            if (int.TryParse(Context.Parameters["Id"].ToString(), out id))
            {
                Model = electronicComponentService.GetById(id);
                SelectedCathegory = Model.ElectronicComponentCategory.Name;
                OnCathegoryInitPrivate();
            }
            return base.Init();
        }

        public override async Task PreRender()
        {

            await base.PreRender();
        }

        public async Task Edit()
        {
            if (Model.Name == null)
            {
                ValidationErrors = new List<string>() { Texts.EnterName };
                return;
            }
            if (Model.ElectronicComponentCategoryId == 0)
            {
                ValidationErrors = new List<string>() { Texts.EnterCathegory };
                return;
            }

            Model.ParametersValues = Helper.ParseAllParameterValuesOrNames(ParametersModels);

            if (UploadedDataSheet != null)
            {
                if (UploadedDataSheet.Files.Count > 0)
                {
                    Model.DatasheetId = UploadedDataSheet.Files[0].FileId;
                }
            }

            electronicComponentService.Update(Model);
            Context.RedirectToRoute("ElectronicComponentManagement");
        }

        private static ElectronicComponentCathegoryModel selectedCathegoryRemembered;
        private static readonly Object OnCathegoryChangedLockObj = new Object();
        public void OnCathegoryChanged()
        {
            lock (OnCathegoryChangedLockObj)
            {
                OnCathegoryChangedPrivate();
            }
        }

        private void OnCathegoryInitPrivate()
        {
            if (Model.ElectronicComponentCategory != null)
            {
                var cathegory = Model.ElectronicComponentCategory;
                if (cathegory != null)
                {
                    if (cathegory.ParametersNames != null)
                    {
                        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;
                    }
                    else
                    {
                        ParametersModels = new List<ElectronicComponentCathegoryForComponentManagementModel>() { };
                    }
                }
            }
        }

        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] = ';';
                    if (cathegory.ParametersNames != null)
                    {
                        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 = ""
                            });
                            i++;
                        }

                        ParametersModels = parameterModelsTmp;

                    }
                    else
                    {
                        ParametersModels = new List<ElectronicComponentCathegoryForComponentManagementModel>() { };
                    }



                }

            }
        }

        public void ProcessUploadedImage()
        {
            Model.ImgBase64 = UploadImage.Files[0].PreviewUrl;
            Model.ImgId = UploadImage.Files[0].FileId;
        }

        public void ProcessUploadedDatasheet()
        {
        }
    }
}
