using DoWhatPlc;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DtWebApi
{
    public partial  class ReadWritePLC
    {
        public static DataTable GetServerStatus()
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "GetServerStatus";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            return new CallWebApi().InvokeWebapiDataTable(controlStr, actionStr, api, type, dic);
        }
        //http://localhost:41186//api//PLC1//Write_TagValueByTagTypeName//?cmd=ConfirmOkGo&opName=OP020&tagValue=True
        //http://localhost:41186//api//PLC1//Read_TagValueByTagTypeName//?cmd=PalletInfo&opName=OP020
        public static object Read_TagValueByTagTypeName(string cmd, string opName)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "Read_TagValueByTagTypeName";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("cmd", cmd);
            dic.Add("opName", opName);

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);
            if(rc.ValueType=="bool" || rc.ValueType == "boolean")
            {
                return Convert.ToInt32(rc.TagValue);
            }
            return rc.TagValue;
        }

        public static object ReadPLC(string tagID)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadPLC";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("tagID", tagID);

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);
            return rc.TagValue;
        }
        public static object ReadPLC(string tagTypeCodeID, string opName)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadPLC_TagTypeCodeID";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("tagTypeCodeID", tagTypeCodeID);
            dic.Add("opName", opName);

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);
            return rc.TagValue;
        }
        public static byte[] ReadByteData_Op(string OpName, string Adr, ushort Length)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadByteData_Op";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("OpName", OpName);
            dic.Add("Adr", Adr);
            dic.Add("Length", Length.ToString());

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);

            return  JsonToByte(rc.TagValue.ToString());
        }

        public static byte[] ReadByteData_Ip(string Ip, string Adr, ushort Length)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadByteData_Ip";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("Ip", Ip);
            dic.Add("Adr", Adr);
            dic.Add("Length", Length.ToString());

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);

            return JsonToByte(rc.TagValue.ToString());
        }

        public static object  WritePLC_Bytes_Op(string OpName, string Adr, byte[] bytes)
        {
            string bytesStr = ByteToJson(bytes);
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "WritePLC_Bytes_Op";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("OpName", OpName);
            dic.Add("Adr", Adr);
            dic.Add("bytes", bytesStr);

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);

            return rc.TagValue;
        }

        public static object WritePLC_Bytes_Ip(string Ip, string Adr, byte[] bytes)
        {
            string bytesStr = ByteToJson(bytes);
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "WritePLC_Bytes_Ip";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("OpName", Ip);
            dic.Add("Adr", Adr);
            dic.Add("bytes", bytesStr);

            var rc = new CallWebApi().InvokeWebapiReturnValueObject(controlStr, actionStr, api, type, dic);

            return rc.TagValue;
        }
        static public string ByteToJson(byte[] bytes)
        {
            string convertJson = System.Text.Encoding.UTF8.GetString(bytes);
            return convertJson;
        }

        static public byte[] JsonToByte(string strjson)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strjson);

            return bytes;
        }


        public static List<ReturnValueObject> ReadPLC(List<string> tagTypeCodeIDList, List<string> opNameList)
        {
            string tagTypeCodeID = JsonConvert.SerializeObject(tagTypeCodeIDList);
            string opName = JsonConvert.SerializeObject(opNameList);
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadPLC_TagTypeCodeIDList";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("tagTypeCodeID", tagTypeCodeID);
            dic.Add("opName", opName);

            var rc = new CallWebApi().InvokeWebapiReturnValueObjectList(controlStr, actionStr, api, type, dic);
            return rc;
        }

        public static List<ReturnValueObject> ReadPLC_GroupData(string opName)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadPLC_GroupData";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("opName", opName);

            return  new CallWebApi().InvokeWebapiReturnValueObjectList(controlStr, actionStr, api, type, dic);
        }
        public static ConcurrentDictionary<string, TagFormat> ReadPLC_OpName(string opName)
        {
            string api = "";
            string type = "GET";
            string controlStr = "PLC1";
            string actionStr = "ReadPLC_OpName";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("opName", opName);

            return new CallWebApi().InvokeWebapiConcurrentDictionaryTagFormat(controlStr, actionStr, api, type, dic);
        }
    }
}
