pragma solidity ^0.4.18;

/*contract Simple {
    uint private a;
    
    function getA() constant returns (uint) {
        return a;
    }
    
    function setP(uint newValue) {
        a = newValue;
    }
}*/

contract MyContract {
    uint currentMonth = 3;
    
    struct Auto {
        string category;
        uint cost;
        uint age;
        bool registr;
        uint strVzn;
        address owner;
    }
    
    struct Drivers {
        string name;
        uint stage;
        uint kDTP;
        uint kSTRF;
        address addr;
    }
    
    struct Drivercard {
            uint number;
            uint day;
            uint month;
            uint year;
            string category;
            address driver;
            bool verify;
        }
        
        Drivers[] public drivers;
        
        mapping (address => uint) public numberDriver;
        mapping (address => uint) public numberAuto;
        mapping (address => uint) public numberCard;
        mapping (uint => address) public numberNum;

        Drivercard[] public drivercards;
        Drivercard[] public verifyDrCards;
 
        Auto[] public autos;
        Auto[] public autosReg;
        address bank = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148;
        address company = 0x583031D1113aD414F02576BD6afaBfb302140225;

        // водитель
        function AddDriver (string _name, uint _stage, uint _kDTP, uint _kSTRF) public {
           drivers.push(Drivers({name : _name, stage : _stage, kDTP : _kDTP, kSTRF : _kSTRF, addr : msg.sender}));
            numberDriver[msg.sender] = drivers.length - 1;
        }
        
        function AddVerifyDriverCard (uint _number, string _category, uint _day, uint _month, uint _year) public {    // Порешать дату
            verifyDrCards.push(Drivercard({number : _number, category : _category,day : _day, month : _month, year : _year, verify : false, driver : msg.sender}));
            //numberCard[_number] = msg.sender;
        } 
        
        function RequestRegistration (string _category, uint _cost, uint _age) public {  // Вписать текущую дату
            autosReg.push(Auto({category : _category, cost : _cost, age : _age, registr : false, owner : msg.sender, strVzn : 0}));
        }
        
        function ValidPeriod() public {
            uint ab = numberCard[msg.sender];
            uint kPen = drivers[numberDriver[msg.sender]].kSTRF;
            if (currentMonth == 1)
            {
                uint diff = 13 - drivercards[ab].month;
            } else {
                 diff = currentMonth - drivercards[ab].month;
            }
            if (drivercards[ab].verify == true && ab < 2 && kPen == 0 ) {
                drivercards[ab].year += 10;
            }
        }
            /*function paymPenalty() public {
                numberDriver[msg.sender];
               // bank.transfer(msg.sender, 10 ether);
            }*/
            
        
        //Дэпос
        
        function Registration (bool _registr) public {
            autosReg[0].registr = _registr;
            if (_registr == true)
            {
                string storage category2 = autosReg[0].category;
                uint cost1 = autosReg[0].cost;
                uint age1 = autosReg[0].age;
                address owner1 = autosReg[0].owner;
                autos.push(Auto({category : category2, cost : cost1, age : age1, registr : true, owner : owner1, strVzn : 0}));
                numberAuto[owner1] = autos.length - 1;
                

                
                delete autosReg[0];
            }else {
                delete autosReg[0];
            }
            Insurance();
        } 
        
        function Insurance() public {
            uint q = numberAuto[msg.sender];
            uint qq = numberDriver[msg.sender];
            int a1 = 1 - int(autos[q].age) / 10;
            if (a1 < 0) {
                a1 *= -1;
            }
            int a3 = (int(autos[q].cost) * a1 / 10);
            int a4 = 2 * int(drivers[qq].kSTRF)/10;
            int a5 = int(drivers[qq].kDTP) - ((2 * int(drivers[qq].stage)) / 10);
            int an = a3 + a4 + a5;
            if (an < 0) {
                an *= -1;
            }
            autos[q].strVzn = uint(an); 
        }
        
        function VerifyDriverCard (bool _verify) public {
            verifyDrCards[0].verify = _verify;
            if (verifyDrCards[0].verify == true) {
                uint number1 = verifyDrCards[0].number;
                bool verify1 = verifyDrCards[0].verify;
                string storage category1 = verifyDrCards[0].category;
                uint day1 = verifyDrCards[0].day;
                uint month1 = verifyDrCards[0].month;
                uint year1 = verifyDrCards[0].year;
                address driver1 = verifyDrCards[0].driver;
                string storage category0 = verifyDrCards[0].category;
                drivercards.push(Drivercard({number : number1, verify : verify1, day : day1, month : month1, year : year1,
                driver : driver1, category : category0}));
                numberCard[driver1] = drivercards.length - 1;
                numberNum[number1] = driver1;
                delete verifyDrCards[0];
            }else {
                delete verifyDrCards[0];
            }
        }
        
        function AddPenalty (uint _number) public {
            drivers[numberDriver[numberNum[_number]]].kSTRF++;
        }
        
        function AddDTP (uint _number) public {
            drivers[numberDriver[numberNum[_number]]].kDTP++;
            // add страховка
        }
        
        

}