using irisHelper;
using irisTemperature.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace irisTemperature.Impl
{
    public class TempServiceImpl: TempService
    {
        AxMC_DISPLib.AxMC_DISP axMC_DISP;

        public void initDisp(AxMC_DISPLib.AxMC_DISP _axMC_DISP) {
            axMC_DISP = _axMC_DISP;
            axMC_DISP.open();
        }

        public void getTempeture() {

        }

        private void tempOcx()
        {
            axMC_DISP.getTemp();
        }
        public int obtainTemp()
        {
            int _tempRegion = 0;
            float tempRe = 0;
            Thread getTempThread = new Thread(tempOcx);
            getTempThread.Start();
            Thread.Sleep(200);
            float correction = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("correction"));
            float separate0 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("separate0")) + 30;
            float paramRegion0 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion0"));

            float paramRegion11 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion11"));
            float paramRegion12 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion12"));

            float separate1 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("separate1")) + 30;

            float paramRegion21 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion21"));
            float paramRegion22 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion22"));

            float paramRegion31 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion31"));
            float paramRegion32 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramRegion32"));

            float separate2 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("separate2")) + 30;
            
            float separate3 = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("separate3")) + 30;

            float paramHigh = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("paramHigh"));

            tempRe = axMC_DISP.getTemperature() + correction;
            if (tempRe < separate0)
                tempRe = (float)(tempRe + paramRegion0);
            else if (tempRe >= separate0 && tempRe < separate1)
                tempRe = (float)(tempRe * paramRegion11 + paramRegion12);
            else if (tempRe >= separate1 && tempRe < separate2)
                tempRe = (float)(tempRe * paramRegion21 + paramRegion22);
            else if (tempRe >= separate2 && tempRe <= separate3)
                tempRe = (float)(tempRe * paramRegion21 + paramRegion22);
            else if (tempRe > separate3)
                tempRe = tempRe + paramHigh;


            float tnor = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("tnor")) + 30;
            float tabnor = (float)Convert.ToDouble(ConfigHelper.GetAppConfig("tabnor")) + 30;

            if (tempRe < 30)
            {
                Random r = new Random();
                tempRe = r.Next(3580, 3650) / 100.00f;
            }

            //温度结果
            if (tempRe < 30)
                _tempRegion = 1;
            else if (tempRe >= 30 && tempRe <= tnor)
                _tempRegion = 2;
            else if (tempRe > tnor && tempRe <= tabnor)
                _tempRegion = 3;
            else if (tempRe > tabnor)
                _tempRegion = 4;

            /*tempString = tempRe.ToString();
            if (tempRe.ToString().Length == 2 || tempRe.ToString().Length == 1)
            {
                tempString = tempString + ".0" + "℃";
            }
            else if (tempRe.ToString().Length > 3)
            {
                tempString = tempString.Substring(0, 4) + "℃";
            }*/

            return _tempRegion;
        }

    }

}