using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Globalization;


namespace golestan
{
    [Serializable]
    class karshenas
    {
        int user;
        int pass;
        const int term1 = 4 + (2 * 20);
        const int ostad1 = 4 + 4 + (4 * 40);
        const int danesh = 4 + 4 + (40 * 5) + 8;
        public karshenas(int a, int a1)
        {
            user = a;
            pass = a1;
        }
        public int USER
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }
        public int PAS
        {
            get
            {
                return pass;
            }
            set
            {
                pass = value;
            }
        }
        const int darse1 = 16 + 2 * 40;
        public void tdars(int num)
        {
            int[] y = new int[2];
            FileStream fs = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("please enter id:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter name:");
                string x1 = Console.ReadLine();
                Console.WriteLine("please enter noe:");
                string x2 = Console.ReadLine();
                Console.WriteLine("please enter vahed :");
                int x3 = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter pishniyaz1:");
                int x4 = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter pishniyaz2:");
                int x5 = int.Parse(Console.ReadLine());
                y[0] = x4;
                y[1] = x5;
                dars s = new dars(x, x1, x2, x3, y);
                fs.Position = (x - 1) * darse1;
                bw.Write(s.A);
                bw.Write(s.NAME);
                bw.Write(s.NOA);
                bw.Write(s.VAHED);
                bw.Write(s[0]);
                bw.Write(s[1]);
            }
            fs.Close();
            bw.Close();

        }

        public void khalidars(int num)
        {
            dars empty = new dars(0, "", "", 0, null);
            FileStream fs = new FileStream("f:\\golestan\\dars.bin", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                fs.Position = i * darse1;
                bw.Write(empty.A);
                bw.Write(empty.NAME);
                bw.Write(empty.NOA);
                bw.Write(empty.VAHED);
                bw.Write(empty[0]);
                bw.Write(empty[1]);
            }
            fs.Close();
            bw.Close();
        }
        public void arayeadoros(int num)
        {

            FileStream fs = new FileStream("f:\\golestan\\araye.bin", FileMode.Create, FileAccess.Write);
            BinaryFormatter bw = new BinaryFormatter();
            Console.WriteLine("please enter number term:");
            int x = int.Parse(Console.ReadLine());
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("please enter number of darse:");
                int x1 = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter number ostad:");
                int x2 = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter number capacity :");
                int x3 = int.Parse(Console.ReadLine());
                araye a = new araye(x, x1, x2, x3,x3);
                bw.Serialize(fs, a);
            }
            fs.Close();
        }
        public void vnomre()
        {
            FileStream fs1 = new FileStream("f:\\golestan\\araye.bin", FileMode.Open, FileAccess.Read);
            BinaryFormatter bw1 = new BinaryFormatter();
            araye r;
            r = (araye)bw1.Deserialize(fs1);
            int t = r.CAPACITY1;
            fs1.Close();
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryFormatter bw = new BinaryFormatter();
            akhz a=new akhz(0,0,0,0);
            string e = "y";
            int i = 0;
            int s;
            double s1;
            while (e == "y" && i < t)
            {
                int flag = 0;
                Console.WriteLine("please enter number of daneshjo:");
                s = int.Parse(Console.ReadLine());
                while (flag == 0 && fs.Position < fs.Length)
                {
                    a =(akhz) bw.Deserialize(fs);
                    i++;
                    if (a.DANESH == s)
                        flag = 1;
                }
                
                if (flag == 0)
                {
                }
                else
                {
                    Console.WriteLine("please enter nomre:");
                    s1 = double.Parse(Console.ReadLine());
                    akhz r1 = new akhz(s, a.DARSE, a.TERM, s1);
                    for (int j = 0; j < i; j++)
                    {
                        bw.Deserialize(fs);
                    }
                    bw.Serialize(fs, r1);
                }
            }
            fs.Close();
            




        }
        public void printlist(int num)
        {
            FileStream fs3 = new FileStream("f:\\golestan\\term.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw3 = new BinaryReader(fs3);
            fs3.Position = term1 * (num - 1);
            int s = bw3.ReadInt32();
            fs3.Close();
            bw3.Close();
            if (num == s)
            {
                int x, x1;
                FileStream fs = new FileStream("f:\\golestan\\araye.bin", FileMode.Open, FileAccess.Read);
                BinaryFormatter bw = new BinaryFormatter();
                FileStream fs1 = new FileStream("f:\\golestan\\ostad.bin", FileMode.Open, FileAccess.Read);
                BinaryReader bw1 = new BinaryReader(fs);
                FileStream fs2 = new FileStream("f:\\golestan\\dars.bin", FileMode.Open, FileAccess.Read);
                BinaryReader bw2 = new BinaryReader(fs);
                araye r;
                while (fs.Position < fs.Length)
                {
                    r = (araye)bw.Deserialize(fs);
                    if (r.TERM == num)
                    {

                        x = r.DARSE;
                        fs2.Position = darse1 * x + 4;
                        x1 = r.OSTAD;
                        fs1.Position = ostad1 * x1 + 4;
                        Console.WriteLine("dars:{0}\tostad:{1}\tcapacity:{2}", bw2.ReadString(), bw1.ReadString(), r.CAPACITY);
                    }
                }
                fs.Close(); 
                fs1.Close();
                bw1.Close();
                fs2.Close();
                bw2.Close();
            }
            else
            {
                Console.WriteLine("chenin termi vojod nadaarad");
            }

        }
        public void taghirzafiat(int x)
        {
            Console.WriteLine("please enter new capacity");
            int x1 = int.Parse(Console.ReadLine());
            int i = 0, f = 0;
            araye r = new araye(0, 0, 0, 0, 0);
            araye r1;
            FileStream fs = new FileStream("f:\\golestan\\araye.bin", FileMode.Open, FileAccess.ReadWrite);
            BinaryFormatter bw = new BinaryFormatter();
            while (fs.Position < fs.Length && f == 0)
            {
                r =(araye) bw.Deserialize(fs);
                i++;
                if (r.DARSE == x)
                {
                    f = 1;
                }
            }
            if (f == 1)
            {
                fs.Position = 0;
                r1 = new araye(r.TERM, r.DARSE, r.OSTAD, x - r.CAPACITY, x1);
                for (int j = 0; j < i; i++)
                {
                    bw.Deserialize(fs);
                }
                bw.Serialize(fs, r1);
                fs.Close();
            }
            else
                Console.WriteLine("chenin darsi pida nashod");
        }
        public void moshahedekarname(int t)
        {
            FileStream fs = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            fs.Position = danesh * (t - 1);
            daneshjo r = new daneshjo(bw.ReadInt32(), bw.ReadString(), bw.ReadString(), bw.ReadString(), bw.ReadString(), bw.ReadString(), bw.ReadInt32(), bw.ReadInt32(), bw.ReadInt32());
            r.karnamekoli();
            fs.Close();
            bw.Close();
        }
        public void mashrotbala()
        {
            FileStream fs = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            while (bw.PeekChar() != -1)
            {
                int x = bw.ReadInt32(); string x1 = bw.ReadString(); string x2 = bw.ReadString(); string x3 = bw.ReadString(); string x4 = bw.ReadString(); string x5 = bw.ReadString(); int x6 = bw.ReadInt32(); int x7 = bw.ReadInt32(); int x8 = bw.ReadInt32();
                if (x5.Substring(0, 10) == "karshenasi")
                {
                    karshenasi r = new karshenasi(x, x1, x2, x3, x4, x5, x6, x7, x8);
                    if (r.tmashrot() > 2)
                        Console.WriteLine("shmare:{0}\tname:{1}\tfamily:{2)", x, x1, x2);
                }
                else
                {
                    karshenasiarshad r = new karshenasiarshad(x, x1, x2, x3, x4, x5, x6, x7, x8);
                    if (r.tmashrot() > 2)
                        Console.WriteLine("shmare:{0}\tname:{1}\tfamily:{2)", x, x1, x2);
                }
            }
        }
                








    }
      
}
