using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace golestan
{
    [Serializable]
    class karshenasi:daneshjo
    {
        public karshenasi(int a1, string a2, string a3, string a4, string a5,string a9, int a6,int a7,int a8) : base(a1, a2, a3, a4, a5,a9, a6,a7,a8) { }
        public void avahed(int tt)
        {

            int t=0,t1=0,i=0,t2=0,t3=0;
            string s;
            FileStream fs1 = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs1);
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.OpenOrCreate, FileAccess.Write);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs2 = new FileStream("f:\\golestan\\akhz.bin", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFormatter bw2 = new BinaryFormatter();
            FileStream fs3 = new FileStream("f:\\golestan\\araye.bin", FileMode.Open, FileAccess.ReadWrite);
            BinaryFormatter bw3 = new BinaryFormatter();

            if (base.moadel(tt - 1) < 12 && tt - 1 > 0)
            {
                t = 14;
            }
            else
                t = 20;
            akhz r;
            akhz r1;
            araye r2;
            while (i < t)
            {
                Console.WriteLine("please enter number darse:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter number ostad:");
                int x1 = int.Parse(Console.ReadLine()); 
                fs1.Position = (16 + 2 * 40)*(x - 1);
                bw1.ReadInt32();
                bw1.ReadString();
                bw1.ReadString();
                t1 = bw1.ReadInt32();
                i += t1;
                t2 = bw1.ReadInt32();
                t3 = bw1.ReadInt32();
                int f=0,f1=0;
                while (fs2.Position < fs2.Length)
                {
                    r = (akhz)bw2.Deserialize(fs);
                    if (r.DANESH == base.A && r.DARSE == t2)
                    {
                        if (r.NOMRE > 10)
                            f = 1;
                    }
                    if (r.DANESH == base.A && r.DARSE == t3)
                        if (r.NOMRE > 10)
                            f1 = 1;
                    if (f == 1 && f1 == 1)
                        break;
                }
                if (f1 == 0 || f == 0)
                    Console.WriteLine("adame raayate pishniyaz");
                else
                {
                    r1 = new akhz(base.A, x, tt, 0);
                    bw.Serialize(fs, r1);
                    int q=0,w=0,a=0,b=0,d=0;
                    while (fs3.Position < fs3.Length)
                    {
                        r2 = (araye)bw3.Deserialize(fs);
                        if (r2.DARSE == x&&r2.TERM==tt)
                        {
                            w = r2.CAPACITY;
                            d = r2.CAPACITY1;
                            a = q;
                            b = r2.OSTAD;
                        }
                        q++;
                    }
                    araye r3=new araye(tt,x,b,d-1,d);

                    for (int j = 0; j < q; j++)
                    {
                        bw3.Deserialize(fs3);

                    }
                    bw3.Serialize(fs3, r3);
                  
                }
                Console.WriteLine("do you want exit(y or n):");
                s = Console.ReadLine();
                if (s[0] == 'y')
                    break; 


            }
            fs1.Close();
            bw1.Close();
            fs.Close();
            fs2.Close();
            fs3.Close();



        }
        public int tmashrot()
        {
            FileStream fs = new FileStream("f:\\golestan\\term.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            int x = 0;
            int t = 0;
            int i = 0;
            while (bw.PeekChar() != -1)
            {
                fs.Position = i * (4 + 2 * 20);
                x = bw.ReadInt32();
                if (base.moadel(x) < 12)
                    t++;
            }
            fs.Close();
            bw.Close();
            return t;
        }
        public void doroseoftade()
        {
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.Open, FileAccess.Read);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs1 = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs1);
            akhz r;
            int t = 1;
            string p;
            while (fs.Position < fs.Length)
            {
                r = (akhz)bw.Deserialize(fs);
                if (r.DANESH == base.A&&r.NOMRE<10)
                {
                    fs.Position = (16 + 2 * 40) * (r.DARSE - 1);
                    bw1.ReadInt32();
                    p = bw1.ReadString();
                    bw1.ReadString();
                    t = bw1.ReadInt32();                 
                    Console.WriteLine("darse:{}\tnomre{1}", p, r.NOMRE);
                }
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
            
        }

    }
}
