using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Http.Headers;
using VideoLibrary;

namespace WDT_Project
{
    public class Line_Notify
    {
        //public string token_GropA = "biVeLL1cyabxhMslTq9UFz2cpEDQwC2YDGXXSKZmoRF";
        //public string token_GropB = "IGenlYLVUD77UuNS1bdHDw7joyLIdnlvsAHnb69M583";
        public static string token_GropA = "Mbl2MU5SZAOfHGhwFpYJUEUOHuakN0vPftmR2PMI0dd"; //Line Notify Kamol_II
        public static string token_GropB = "Mbl2MU5SZAOfHGhwFpYJUEUOHuakN0vPftmR2PMI0dd"; //Line Notify Kamol_II
        //public string token_GropA = "SLmHI7vUiEdh3W9szgSrHCfukMGlBnU6iTzVxJom4zj"; //Line Notify Group Kamol_II
        //public string token_GropB = "SLmHI7vUiEdh3W9szgSrHCfukMGlBnU6iTzVxJom4zj"; //Line Notify Group Kamol_II
        public static string pathPicture = @"C:\TOMAS TECH\Camera Data PLC LINE NOTIFY\Master_Log\";
        public static string destinationPath = @"C:\TOMAS TECH\Camera Data PLC LINE NOTIFY\Picture_Log\";
        /// <summary>
        /// 
        /// </summary>
        private HttpClient httpClient;

        public void LineNotify(string Gruop, string message)
        {
            try
            {
                string tokenGroup = "";
                if (Gruop == "A")
                {
                    tokenGroup = token_GropA;
                }
                if (Gruop == "B")
                {
                    tokenGroup = token_GropB;
                }
                var request = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
                var postData = string.Format("message={0}", message);
                var data = Encoding.UTF8.GetBytes(postData);

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.Headers.Add("Authorization", "Bearer " + tokenGroup);
                var stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public async Task SendImageToGroup(string Gruop, string type, string pictureName)
        {
            try
            {
                string tokenGroup = "";
                string imagePath = "";
                string message = "";
                string groupId = "";

                if (pictureName == string.Empty)
                {
                    return;
                }

                if (Gruop == "A")
                {
                    tokenGroup = token_GropA;
                    message = "PIC_Machine001";
                    //if(type == "PIC") {
                    //imagePath = @"D:\MACHINE_IMAGE\MC001\001_1.jpg";
                    imagePath = pathPicture + pictureName;
                    //}
                    //else if (type == "V")
                    //{
                    //    imagePath = @"D:\MACHINE_IMAGE\MC001\BUSHING_ER001.mp4";
                    //}

                    groupId = "Group_A";
                }
                if (Gruop == "B")
                {
                    message = "PIC_Machine002";
                    tokenGroup = token_GropB;
                    //imagePath = @"D:\MACHINE_IMAGE\MC002\002_1.jpg";
                    imagePath = pathPicture + pictureName;
                    groupId = "Group_B";
                }
                string linenoti_token = tokenGroup;
                string linenoti_url = "https://notify-api.line.me/api/notify";
                HttpClient Client = new HttpClient();
                Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("multipart/form-data"));
                Client.DefaultRequestHeaders.Add("Authorization", "Bearer " + linenoti_token);
                string msg = message;
                //System.IO.Stream fstream = System.IO.File.OpenRead(imagePath);
                System.IO.Stream fstream = System.IO.File.OpenRead(imagePath);

                MultipartFormDataContent formdata = new MultipartFormDataContent();
                formdata.Headers.ContentType.MediaType = "multipart/form-data";
                formdata.Add(new StreamContent(fstream), "imageFile", imagePath);
                formdata.Add(new StringContent(msg), "message");
                HttpResponseMessage response = new HttpResponseMessage();
                String responsemsg = "";
                try
                {
                 
                    response = await Client.PostAsync(linenoti_url, formdata);
                    response.EnsureSuccessStatusCode();
                    Client.Dispose();
                    responsemsg += "status :\n";
                    responsemsg += response.StatusCode.ToString() + "\n";
                    responsemsg += "\nheader :\n";
                    foreach (var header in response.Headers)
                    {
                        responsemsg += header.Key.ToString() + ": " + string.Join(",", header.Value) + "\n";
                    }
                    responsemsg += "\ncontent :\n";
                    responsemsg += response.Content.ReadAsStringAsync().Result;
                }
                catch (Exception ex)
                {
                    responsemsg += ex.ToString();
                }



                string moveTo = destinationPath + pictureName;
                if (File.Exists(moveTo) && File.Exists(imagePath))
                {
                    File.Delete(moveTo);
                }               
                //moving file
                File.Move(imagePath, moveTo);
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}
