﻿/*
        Copyright 2015 Verloka Vadim, http://ogy.pp.ua

        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using JPDeploy.assets.core;
using System.Net;
using System.Threading;

namespace JPDeploy.assets.pages.install
{
    public partial class installGetVersions : Page
    {
        AppInstall App;
        List<core.Version> Versions;

        public installGetVersions(AppInstall App)
        {
            InitializeComponent();
            this.App = App;
            Versions = new List<core.Version>();
        }

        void LoadVersionList()
        {
            if (!Connection.Yes())
            {
                //TODO ERROR
                return;
            }

            string[] list;
            try
            {
                WebClient client = new WebClient();
                list = client.DownloadString("https://jpassword.pp.ua/deploy/index.text").Split('\n');
            }
            catch
            {
                //TODO ERROR
                return;
            }
            foreach (var item in list)
            {
                core.Version v = new core.Version();

                int.TryParse(item.Split('.')[0], out v.Major);
                int.TryParse(item.Split('.')[1], out v.Minor);

                string[] files;
                try
                {
                    WebClient client = new WebClient();
                    files = client.DownloadString("https://jpassword.pp.ua/deploy/" + v.Major + "." + v.Minor + "/index.text").Split('\n'); ;
                }
                catch { continue; }
                v.Files = new List<VersionFiles>();
                foreach (var itemf in files)
                {
                    string[] ad = itemf.Split('+');
                    v.Files.Add(new VersionFiles()
                    {
                        Url = new Uri("https://jpassword.pp.ua/deploy/" + v.Major + "." + v.Minor + "/" + ad[0]),
                        Folder = ad[1],
                        Name = ad[2],
                        Extension = ad[3]
                    });
                }

                Versions.Add(v);
            }

            Finish();
        }
        void Finish()
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                App.mw.btnPrev.IsEnabled = true;

                files.ItemsSource = Versions;

                loader.Visibility = System.Windows.Visibility.Collapsed;
                gFiles.Visibility = System.Windows.Visibility.Visible;
            }));
        }

        private void pageLoaded(object sender, RoutedEventArgs e)
        {
            Thread t = new Thread(LoadVersionList);
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        private void filesSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (files.SelectedIndex >= 0)
            {
                App.version = files.SelectedItem as core.Version;
                App.mw.btnNext.IsEnabled = true;
            }
        }
    }
}
