using Remontor.Setting;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Remontor.Picter
{
    internal class PicLoaderTask
    {
        private string Name;
        private WebClient Client;
        private Image Img;
        private PictureBox ImagePanel;
        public PicLoaderTask(string name, PictureBox imagePanel) 
        {
            ImagePanel = imagePanel;
            this.Name = name;
            Client = new WebClient();
            Client.DownloadDataCompleted += Client_DownloadDataCompleted;
            Client.Credentials = new NetworkCredential();
            Client.UseDefaultCredentials = true;
            string urlSetting = SGlobalSetting.settingApp.urlPhoto;
            urlSetting = urlSetting.Replace("{#Login}", name);
            try
            {
                Uri url = new Uri(urlSetting);       
                Client.DownloadDataAsync(url);
            }
            catch
            {
                MessageBox.Show("Неверная строка запроса фотографии");
            }
        }

        private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Img = new Bitmap(new MemoryStream(e.Result));
                ImagePanel.Image = Img;
                SPicManager.WriteResult(new PicBase(Img,Name));
            }

            
           // throw new NotImplementedException();
        }
    }
}
