/*****************************************************************************\
In parts
Copyright (C) 2010 by LMS
In parts
Copyright (C) 2010 by Threepwood
 * 
 * meinfleisch@gail.com
 * 
 * 
 * v1.3    <-------------------------------------------------!!!!

Door + ini example for TheChopShop by LMS 13.03.2010

You may modifiy this if you keep this header

The author of this script can't be held liable for any damages caused by this script!

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

\**********************************************************************/


using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using GTA;  // Import GTA namespace to use its classes, methods etc.



namespace Test
{
    class EPM8 : Script
    {
        // my variables etc
        string vehLine;
        public int door;
        public int doorNumb = 0;
        public string keyValue;
        public string lineValue;
        public string line;





        // Constructor
        public EPM8()
        {
            this.Tick += new EventHandler(ChopShopExample_Tick2); // Out of order for testing
        }



        private void ChopShopExample_Tick2(object sender, EventArgs e) // Tick event handler
        {
            // Check whether local player is in any vehicle
            if (Player.Character.isInVehicle())
            {
                Vehicle veh = Player.Character.CurrentVehicle;

                vehLine = ReadSettings(veh.Model.Hash.ToString(), 1, doorNumb.ToString(), "./scripts/moveDoors.ini");
                if (vehLine != "NADA")
                {

                    string[] vehInfo = vehLine.Split(',');
                    int door = Convert.ToInt32(vehInfo[1]); // Converts string to int32
                    int door2 = Convert.ToInt32(vehInfo[2]); // Converts string to int32
                    float speedOpen = float.Parse(vehInfo[2]);
                    float speedClose = float.Parse(vehInfo[3]);
                    /* Check for speed spoiler switch */
                    if (vehInfo[22] == "1")
                    {
                        // Game.DisplayText("offen", 30000);
                        // veh.Door((VehicleDoor)0).Angle = veh.Door((VehicleDoor)door2).Angle;
                        if (veh.Door((VehicleDoor)0).isOpen)
                        {
                            veh.Door((VehicleDoor)door2).Angle = veh.Door((VehicleDoor)0).Angle;
                            //  veh.Door((VehicleDoor)door2).Open();
                        }
                        if (!veh.Door((VehicleDoor)0).isOpen)
                        {
                            veh.Door((VehicleDoor)door2).Close();
                            //  veh.Door((VehicleDoor)door2).Open();
                        }
                        if (Player.Character.isGettingIntoAVehicle)
                        {

                            veh.Door((VehicleDoor)door2).Angle = veh.Door((VehicleDoor)0).Angle;
                        }
                    }
                }
            }
        }


        /*
         *  INI READER
         * 
         * */
        private string ReadSettings(string key, int keyT, string which1, string iniFilePath)
        {
            lineValue = "no";
            string chkThis;

            using (FileStream filestream = new FileStream(iniFilePath, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader reader = new StreamReader(filestream))
                {
                    bool foundIt = false;
                    while (((line = reader.ReadLine()) != null) & (foundIt != true))
                    {
                        if (!line.StartsWith("#"))
                        {
                            string[] vehCheck = line.Split(',');
                            string whichVal;
                            if (keyT == 10)
                            {
                                chkThis = vehCheck[10];
                                whichVal = "1";
                            }
                            else
                            {
                                chkThis = vehCheck[keyT];
                                whichVal = which1;

                            }
                            if ((vehCheck[0] == key) & (chkThis.ToString() == whichVal))
                            {
                                keyValue = line;
                                foundIt = true;
                                reader.Close();
                                break;
                            }
                            else
                            {
                                keyValue = "NADA";
                                foundIt = false;
                            }
                        }
                    }
                    reader.Close();
                }
            }
            return keyValue;
        }

    }
}


/* content of moveDoors.ini:
#Line format:
#CarID,doorID,speedOpen,speedClose,keyOpen,keyClose,keyActive,speedActive,brakeActive,debugActive
# count the entry position beginning with 0!! CarID = 0. doorID = 1 and so on.
#do NOT insert any special character, no whitespace, blank, nothing else but alphanumerics and "," as separators. 
#One line per car and door, means use a second line for a second opening door!
#first found entry counts. Door IDs:
#0 - Left front door
#1 - Right front door
#2 - Left rear door
#3 - Right rear door
#4 - Hood
#5 - Trunk
#keyActive and speedActive are switches!
previon,0,20,20,I,K,1,1,0,1
cgt,2,20,20,I,K,1,1,0,1
agatha,0,20,20,I,K,1,1,0,1
toto,0,20,20,I,K,1,1,0,1
 */

