using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Rate_Reading
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            
        }

        /*  double Rate_back(int wellNo, double time)
          {
              string[] RateList = File.ReadAllLines("rate.dat");
              int Row = RateList.Length; //Numbers of lines in file
              double Rate=0;
              double ts, tf, cr;
            
              for (int j = 0; j < Row; j++)
              {
                  if (wellNo == Convert.ToInt32(RateList[j].Split(',')[0]))//kuyunun oldugu satiri bul;
                  {
                      int nos = Convert.ToInt32(RateList[j].Split(',')[1]);//number of rate steps
                      int eos;//number of digits sepwrated by ',' up to end of steps
                      eos = nos * 3 - 1;

                      for (int i = 2; i <= eos; i = i + 3)
                      {
                          ts = Convert.ToDouble(RateList[j].Split(',')[i]);
                          tf = Convert.ToDouble(RateList[j].Split(',')[i + 1]);
                          cr = Convert.ToDouble(RateList[j].Split(',')[i + 2]);
                          if (ts <= time && tf >= time)
                          {
                              Rate = cr;
                              //Nt = tf;
                          }
                          else //if there is no rate at the time 
                          {
                              Rate = 0;
                              //Nt = Convert.ToDouble(RateList[i].Split(',')[i + 3]);
                          }
                      }
                  }
                  else // if there is no info about well 
                  {
                      Rate = 0;
                     // Nt = 1000;//TMAX burda geri donecek
                  }
              }

              return Rate;
          }
          */
        private void RateRead(int wellNo, double time, out double Rate, out double Nt)
        {
            Rate=0;
            Nt=10;
            string[] RateList = File.ReadAllLines("rate.dat");
            int Row = RateList.Length; //Numbers of lines in file
            double ts,tf,cr,tfOi,tsOi;//ts start time, tf final time, cr current rate
            int ok = 0;
            for (int j = 0; j < Row; j++)
            {
                if (ok == 1) break;
               int xx = Convert.ToInt32(RateList[j].Split(',')[0]);
                if (wellNo == xx)//kuyunun oldugu satiri bul;
                {
                    int nos = Convert.ToInt32(RateList[j].Split(',')[1]);//number of rate steps
                    int eos;//number of digits sepwrated by ',' up to end of steps
                    eos = nos * 3 + 1;
                    
                    
                    for (int i = 2; i < eos; i = i + 3)//satir icinde irlei get
                    {
                        
                        ts = Convert.ToDouble(RateList[j].Split(',')[i]);
                        tf = Convert.ToDouble(RateList[j].Split(',')[i + 1]);
                        cr = Convert.ToDouble(RateList[j].Split(',')[i + 2]);
                     
                        if (ts <= time && tf >= time)
                        {
                            Rate = cr;
                            Nt = tf;
                            ok = 1;
                            //return Rate;
                        }
                        else //if there is no rate at the time 
                        {
                            Rate = 0;
                            for (int ti = 3; ti < eos-1; ti = ti + 3)
                            {
                                tfOi = Convert.ToDouble(RateList[j].Split(',')[ti]);//tfinal of i
                                tsOi = Convert.ToDouble(RateList[j].Split(',')[ti + 2]);//t start of i+1
                                if (tfOi <= time && tsOi >= time)
                                {
                                    Nt = tsOi;
                                    ok = 1;
                                }
                            }//for
                          }//else
                          if (ok == 1) break;
                       }//setr icinde gez
                       
                }//kolonlarda axdar

                    
            }//for
            
                if(ok==0) // if there is no info about well 
                {
                    Rate = 0;
                    Nt = 1000;//TMAX burda geri donecek
                    for (int j = 0; j < Row; j++)
                    {
                        int kuyuNo = Convert.ToInt32(RateList[j].Split(',')[0]);
                        double tstar = Convert.ToInt32(RateList[j].Split(',')[2]);
                        if (kuyuNo==wellNo & time<tstar)
                            Nt = Convert.ToInt32(RateList[j].Split(',')[2]);
                        
                    }
                 }
            
                
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            int noWell;
            double vaxt,debi,sonrakiT;
            
            noWell = Convert.ToInt32(Wellnou.Text);
            vaxt=Convert.ToDouble(TimeA.Text);

            RateRead( noWell, vaxt, out debi, out sonrakiT);

            ResultDebi.Text=debi.ToString();
            ResulttVatx.Text = sonrakiT.ToString();

        }
    }
}