using CCWin;
using Newtonsoft.Json.Linq;
using SufeiUtil;
using SXLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;

namespace MOKA_Factory_Tools
{
    public partial class CreateDBManually : Skin_Color
    {
        string url;
        string order;
        MidList midList1 = new MidList();
        MidAddress midAddress1 = new MidAddress();
        SQLiteConnection errorDBNow;
        SQLiteConnection ReportDB;
        string dbPath;
        public CreateDBManually(string getMidInfoUrl_Test, System.Data.SQLite.SQLiteConnection errorDB)
        {
            InitializeComponent();
            url = getMidInfoUrl_Test;
            errorDBNow = errorDB;
        }

        private void SubmitMIDInfo_Click(object sender, EventArgs e)
        {
            if (Order_Text.Text.Trim().Length > 0)
            {
                dbPath = AppDomain.CurrentDomain.BaseDirectory + "\\Keys\\" + Order_Text.Text.Trim() + ".db";
                order = Order_Text.Text.Trim();
                if (CommonMethod.GetMidInfo(url, order, TestMode.MAC, "", "", errorDBNow, out midList1))
                {   
                    if (CommonMethod.GetMidAddress2(midList1, order, errorDBNow, out List<MidAddress> midAddress2))
                    {
                        foreach(var midAddress1 in midAddress2)
                        {
                            SoftwareVersion_Text.Text = midAddress1.version;
                            ClientType_Text.Text = midAddress1.ctype;
                            OrderQuantity.Text = midAddress1.number;
                            ProjectID_Text.Text = midAddress1.pid;
                            Download DownloadPage = new Download(errorDBNow, midAddress1, comboBox1.SelectedIndex, midList1, null);
                            DownloadPage.ShowDialog();
                        }
                        if(File.Exists(dbPath))
                        {
                            ReportDB = new SQLiteConnection("data source=" + dbPath);
                            ReportDB.Open();
                            if (SQLiteHelper.WholeCheckDownloadStatus(ReportDB, order))
                            {
                                if (CommonMethod.ReportDownloadStatus(midList1.host, order, errorDBNow))
                                    MessageBox.Show(LResource.DownloadSuccess);
                                else
                                    MessageBox.Show(LResource.DownloadReportFail);
                            }
                            ReportDB.Close();
                        }                       
                    }
                    else
                        MessageBox.Show(LResource.GetMidAdresserror);

                }
                else
                    MessageBox.Show(LResource.GetMidInfoerror);

            }
            else
                MessageBox.Show("请输入订单号!");
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            dbPath = AppDomain.CurrentDomain.BaseDirectory + "\\Keys\\" + Order_Text.Text.Trim() + ".db";
            ReportDB = new SQLiteConnection("data source=" + dbPath);
            bool result = true;
            try
            {
                ReportDB.Open();
                string count;
                if (SQLiteHelper.GetEppormDataCount(ReportDB, out count))
                    progressBar1.Maximum = Convert.ToInt32(count) + 1;
                else
                    goto end;
                if(SQLiteHelper.GetEppormUploadedDataCount(ReportDB, out count))
                    progressBar1.Value= Convert.ToInt32(count);
                else
                    goto end;
                List<string> SNs = new List<string>();
                do
                {
                    //int n = 0;
                    List<PostDateClass> postList = new List<PostDateClass>();
                    
                    if (!SQLiteHelper.GetEppormDataList(ReportDB, out postList,out SNs))
                    {
                        goto end;
                    }
                    if (SNs.Count==0)
                    {
                        break;
                    }
                    if (!CommonMethod.UploadPreloadCopyResultList(midAddress1.host, postList, ReportDB, errorDBNow, false, ""))//
                    {
                        MessageBox.Show("上报异常!");
                        goto end;
                    }
                    else
                    {
                        SQLiteHelper.UpdateReportListStatus(ReportDB, SNs);          
                        
                    }
                } while (SNs.Count>0);
                MessageBox.Show("上报成功!");
                result = false;
                end:
                if(result)
                    MessageBox.Show("error!");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CreateDBManually_FormClosing(object sender, FormClosingEventArgs e)
        {
            errorDBNow.Close();
            GC.Collect();
        }

        private void CreateDBManually_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }
    }
}