using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using SDKCSharp.Client;
using SDKCSharp.Common;
using SDKCSharp.Model;
using SDKCSharp.Request;
using SDKCSharp.Response;
using SDKCSharp.Utility;

namespace SDKTest
{
    class MainClass
    {
        static string url = "http://localhost:8081/api";
        //static string appId = "201904035630907729292csharp";
        static string appId = "202412201319756154208780288";
        // 开发者私钥, PKCS1 2048
        //static string privateKey = "MIIEowIBAAKCAQEA5+OvJxeSzf44NxQ/cl7Ii+BzPg2k6sRcvH4ffOtU5Dzq1/oEvg02nxIhmwOHBZmjbmuUu0aLsfglUTAwqfXftfAKZidshsgj9NNh0/kxk0avRZ1UoljWGz/FxVZA0ogbxxhohPZ9jWcD+eBQcIwF2DtHfAJqWWZrYFnCMeHD8mPzxo2kwXSvDzi0vf9I2tKiYvNG26a9FqeYtPOoi81sdS3+70HOMdxP8ejXtyfnKpKz7Dx506LCIRS5moWS3Q5eTLV3NGX/1CSJ8wpQA2DAQTjVhX5eVu7Yqz12t8W+sjWM/tHUR6cgwYYR10p7tSCeCPzkigjGxKm4cYXWtATQJQIDAQABAoIBAHFDsgrrJca+NKEan77ycwx3jnKx4WrWjOF4zVKL9AQjiSYDNgvKknJyPb3kpC/lEoHdxGERHSzJoxib7DkoIqRQYhPxj73pxj5QfYk3P7LLJNNg/LTrpXDb3nL8JV9wIflGf87qQvstZTDJEyFWE4jBs7Hr0BxovWvri8InnzkmERJ1cbGJgNHe1Y3Zo2tw0yaHxQCxLuajP+notRZhD9bEp7uKeI0w9AvlW6k8m/7y10F0BK/TlyW8rQiEC391yOiRYoMcUh4hd2Q9bMx3jngZgX8PXIvZZcup4/pvWlv1alwhB2tsnLdazP62r1MO80vLyLunzGO+7WwCjEYlVaECgYEA+lQRFmbhKaPuAuXMtY31Fbga8nedka5TjnEV7+/kX+yowE2OlNujF+ZG8UTddTxAGv56yVNi/mjRlgD74j8z0eOsgvOq9mwbCrgLhLo51H9O/wAxtb+hBKtC5l50pBr4gER6d8W6E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LKP+LugMGIgTWYQKBgCPzZil24ZD4QmUtxZXTe17xmJrAekIpJ0n3B4WT+nuXAHiV3L8WD6VSh0T6HJXI/4m4NGdenEYqAp4wbAHxJshaFsoHgdt9ndX1SJ8JZ/VhjeEKx7xV5s5gHLFXWsnOsrByifj2KS2lLujxJuZYoA2cHUE0kvaa2mV4rg+trUuF";
        // 开放平台给的公钥
        // 前往SOP-ADMIN，ISV管理--秘钥管理，选择PKCS1，生成平台提供的公私钥，然后把【平台公钥】放到这里
        static string publicKeyPlatform = "";

        // 从文件中加载
        //static string filePath = "/Users/thc/logs/priKey.txt";
        //static OpenClient client = new OpenClient(url, appId, filePath, true);

        // 声明一个就行
        static OpenClient client = new OpenClient(url, appId, privateKey, publicKeyPlatform);


        public static void Main(string[] args)
        {
            TestGet();
            Console.WriteLine("--------------------");
            //TestUpload();
        }

        // 标准用法
        private static void TestGet()
        {
            // 创建请求对象
            GetProductRequest request = new GetProductRequest();
            // 请求参数
            GetProductModel model = new GetProductModel();
            model.Id = 1;
            request.BizModel = model;

            // 发送请求
            Result<GetProductResponse> result = client.Execute(request);
          
            if (result.IsSuccess())
            {
                // 返回结果
                Console.WriteLine("成功！response:{0}\n响应原始内容:{1}", JsonUtil.ToJSONString(result), result.Data);
            }
            else
            {
                Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
                    result.Code, result.Msg, result.SubCode, result.SubMsg);
            }
        }


        private static void TestUpload()
        {
            DemoFileUploadRequest request = new DemoFileUploadRequest();

            DemoFileUploadModel model = new DemoFileUploadModel
            {
                Remark = "上传文件参数"
            };
            request.BizModel = model;

            string workDir = Environment.CurrentDirectory;
            int index = workDir.LastIndexOf(@"/bin/Debug", StringComparison.Ordinal);
            string root = workDir.Substring(0, index);

            Console.WriteLine("当前目录{0}", root);

            // 文件上传
            // 将当前目录下的两个文件上传到服务器
            request.AddFile(new UploadFile("file1", root + "/file1.txt"));
            request.AddFile(new UploadFile("file2", root + "/file2.txt"));

            Result<DemoFileUploadResponse> result = client.Execute(request);
            if (result.IsSuccess())
            {
                List<DemoFileUploadResponse.FileMeta> responseFiles = result.Data.Files;
                Console.WriteLine("您上传的文件信息：");
                responseFiles.ForEach(file =>
                {
                    Console.WriteLine("上传的文件名：{0}，文件大小：{1}，文件内容：{2}", file.Filename, file.Size, file.Content);
                }
                );
            }
            else
            {
                Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
                     result.Code, result.Msg, result.SubCode, result.SubMsg);
            }
        }
    }
}
