Bed_EPOCH = [[[0,0,0.6],[0,0,-0.1]],[[0,0,0.6],[0,0,0.9]],[[0,0,0.5],[1.1,0,0.5]],[[0,0,0.5],[-0.7,0,0.5]],[[0,0,0.5],[0,1.4,0.5]],[[0,0,0.5],[0,-1.4,0.5]]];
Fridge_EPOCH = [[[0,0,1.1],[0,0,-0.1]],[[0,0,1.1],[0,0,2.1]],[[0,0,1.1],[0.5,0,1.1]],[[0,0,1.1],[-0.5,0,1.1]],[[0,0,1.1],[0,0.4,1.1]],[[0,0,1.1],[0,-0.9,1.1]]];
Shelf_EPOCH = [[[0,0,1],[0,0,-0.1]],[[0,0,1],[0,0,1.8]],[[0,0,1],[0.5,0,1]],[[0,0,1],[-0.5,0,1]],[[0,0,1],[0,0.2,1]],[[0,0,1],[0,-0.6,1]]];
Couch_EPOCH = [[[0,0,0.4],[0,0,-0.1]],[[0,0,0.4],[0,0,1.2]],[[0,0,0.4],[1.3,0,0.4]],[[0,0,0.4],[-0.3,0,0.4]],[[0,0,0.4],[0,1.4,0.4]],[[0,0,0.4],[0,-1.4,0.4]]];
wardrobe_epoch = [[[0,0,1.3],[0,0,-0.1]],[[0,0,1.3],[0,0,2.3]],[[0,0,1.3],[0.7,0,1.3]],[[0,0,1.3],[-0.7,0,1.3]],[[0,0,1.3],[0,0.4,1.3]],[[0,0,1.3],[0,-0.6,1.3]]];
cooker_epoch = [[[0,0,0],[0,0,-0.7]],[[0,0,0],[0,0,0.7]],[[0,0,0],[0.4,0,0]],[[0,0,0],[-0.4,0,0]],[[0,0,0],[0,0.5,0]],[[0,0,0],[0,-0.7,0]]];
Chair_EPOCH = [[[0,0,0.5],[0,0,-0.1]],[[0,0,0.5],[0,0,1.5]],[[0,0,0.6],[0.6,0,0.6]],[[0,0,0.6],[-0.6,0,0.6]],[[0,0,0.6],[0,0.6,0.6]],[[0,0,0.5],[0,-0.6,0.5]]];
Filing_epoch = [[[0,0,0.8],[0,0,-0.1]],[[0,0,0.8],[0,0,1.6]],[[0,0,0.8],[0.3,0,0.8]],[[0,0,0.8],[-0.3,0,0.8]],[[0,0,0.8],[0,0.4,0.8]],[[0,0,0.8],[0,-0.5,0.8]]];
Pelican_EPOCH = [[[0,0,0.2],[0,0,-0.1]],[[0,0,0.2],[0,0,0.7]],[[0,0,0.2],[0.9,0,0.2]],[[0,0,0.2],[-0.9,0,0.2]],[[0,0,0.2],[0,0.6,0.2]],[[0,0,0.2],[0,-0.4,0.2]]];
Table_EPOCH = [[[0,0,0.5],[0,0,-0.1]],[[0,0,0.5],[0,0,0.8]],[[0,0,0.5],[1.1,0,0.5]],[[0,0,0.5],[-1,0,0.5]],[[0,0,0.5],[0,0.5,0.5]],[[0,0,0.5],[0,-0.7,0.5]]];
Locker_EPOCH = [[[0,0,1.1],[0,0,-0.1]],[[0,0,1.1],[0,0,2.1]],[[0,0,1.1],[1,0,1.1]],[[0,0,1.1],[-0.9,0,1.1]],[[0,0,1.1],[0,0.4,1.1]],[[0,0,1.1],[0,-0.5,1.1]]];
ToolRack_EPOCH = [[[0,0,1.5],[0,0,-0.1]],[[0,0,1.5],[0,0,2.3]],[[0,0,1.5],[1,0,1.5]],[[0,0,1.5],[-0.9,0,1.5]],[[0,0,1.5],[0,0.05,1.5]],[[0,0,1.5],[0,-0.4,1.5]]];
Shoebox_EPOCH = [[[0,0,0.1],[0,0,-0.1]],[[0,0,0.1],[0,0,0.3]],[[0,0,0.1],[0.3,0,0.1]],[[0,0,0.1],[-0.3,0,0.1]],[[0,0,0.1],[0,0.2,0.1]],[[0,0,0.1],[0,-0.2,0.1]]];
Tarp_EPOCH = [[[0,0,0.2],[0,0,-0.1]],[[0,0,0.2],[0,0,1]],[[0,0,0.2],[0.9,0,0.2]],[[0,0,0.2],[-0.9,0,0.2]],[[0,0,0.2],[0,1,0.2]],[[0,0,0.2],[0,-0.9,0.2]]];
Freezer_EPOCH = [[[0,0,0.5],[0,0,-0.2]],[[0,0,0.5],[0,0,1.1]],[[0,0,0.5],[0.7,0,0.5]],[[0,0,0.5],[-0.7,0,0.5]],[[0,0,0.5],[0,0.5,0.5]],[[0,0,0.5],[0,-0.6,0.5]]];
Cabinet_EPOCH = [[[0,0,0],[0,0,-0.4]],[[0,0,0],[0,0,0.4]],[[0,0,0],[0.2,0,0]],[[0,0,0],[-0.2,0,0]],[[0,0,0],[0,0.3,0]],[[0,0,0],[0,-0.3,0]]];

EPOCH_buildClasses = [
"Shelf_EPOCH",
"Fridge_EPOCH",
"Bed_EPOCH",
"Couch_EPOCH",
"wardrobe_epoch",
"cooker_epoch",
"Chair_EPOCH",
"Filing_epoch",
"Pelican_EPOCH",
"Table_EPOCH",
"Locker_EPOCH",
"ToolRack_EPOCH",
"Shoebox_EPOCH",
"Tarp_EPOCH",
"Freezer_EPOCH",
"Cabinet_EPOCH"
];

EPOCH_buildClassesCount = count EPOCH_buildClasses - 1;
Epoch_selectedBuildClasIndex = 0;
EPOCH_selectedBuildClass = EPOCH_buildClasses select 0;

EPOCH_objPOS = player modelToWorld[0, 5, 0.5];
if !(surfaceIsWater _currentPos) then {
    EPOCH_objPOS = ATLtoASL _currentPos;
};

{
    detach _x;
} forEach attachedObjects player;

EPOCH_currentBuildObj = EPOCH_selectedBuildClass createVehicle EPOCH_objPOS;
EPOCH_currentBuildObj setPosATL EPOCH_objPOS;
comment "EPOCH_currentBuildObj attachTo[player];";
EPOCH_selectedEditIndex = 0;

EPOCH_fnc_AGLToATL = {
    _pos = _this;
    if !(surfaceIsWater _pos) then {_pos = ASLToATL _pos};
    _pos
};

EPOCH_KeyDownFunctionhandler = {
    _dikCode = _this select 1;
    _shift = _this select 2;
    _ctrl = _this select 3;
    _alt = _this select 4;
    _handled = false;

    switch (_dikCode) do {

        case 0xC7 : {
            _index = missionNamespace getVariable ["EPOCH_selectedEditIndex",0];
            _index = _index + 1;
            missionNamespace setVariable ["EPOCH_selectedEditIndex",_index];
            hint str(_index);
            _handled = true;
        };
        case 0xCF : {

            _index = missionNamespace getVariable ["EPOCH_selectedEditIndex",0];
            _index = _index - 1;
            missionNamespace setVariable ["EPOCH_selectedEditIndex",_index];
            hint str(_index);
            _handled = true;
        };

        case 0xC8 : {
            hint "UP";
            _extents = missionNamespace getVariable [EPOCH_selectedBuildClass,[[[0,0,0],[0,0,0]]]];
            _new = [];
            {
                if (_foreachindex == EPOCH_selectedEditIndex) then {
                    _extents1 = _x select 0;
                    _extents2 = _x select 1;
                    if (_ctrl) then {
                        _new pushBack ([[_extents1 select 0, _extents1 select 1, (_extents1 select 2)+0.1],[_extents2 select 0, _extents2 select 1, (_extents2 select 2)+0.1]]);
                    } else {
                        _new pushBack ([[_extents1 select 0, _extents1 select 1, (_extents1 select 2)],[_extents2 select 0, _extents2 select 1, (_extents2 select 2)+0.1]]);
                    };
                } else {
                    _new pushBack _x;
                };
            } forEach _extents;
            missionNamespace setVariable [EPOCH_selectedBuildClass,_new];
            _handled = true;
        };
        case 0xD0     : {
            hint "DOWN";
            _extents = missionNamespace getVariable [EPOCH_selectedBuildClass,[[[0,0,0],[0,0,0]]]];
            _new = [];
            {
                if (_foreachindex == EPOCH_selectedEditIndex) then {
                    _extents1 = _x select 0;
                    _extents2 = _x select 1;
                    if (_ctrl) then {
                        _new pushBack ([[_extents1 select 0, _extents1 select 1, (_extents1 select 2)-0.1],[_extents2 select 0, _extents2 select 1, (_extents2 select 2)-0.1]]);
                    } else {
                        _new pushBack ([[_extents1 select 0, _extents1 select 1, (_extents1 select 2)],[_extents2 select 0, _extents2 select 1, (_extents2 select 2)-0.1]]);
                    };
                    
                } else {
                    _new pushBack _x;
                };
            } forEach _extents;
            missionNamespace setVariable [EPOCH_selectedBuildClass,_new];
            _handled = true;
        };
        case 0xCB     : {
            hint "LEFT";
            _extents = missionNamespace getVariable [EPOCH_selectedBuildClass,[[[0,0,0],[0,0,0]]]];
            _new = [];
            {
                if (_foreachindex == EPOCH_selectedEditIndex) then {
                    _extents1 = _x select 0;
                    _extents2 = _x select 1;

                    if (_ctrl) then {
                        _new pushBack [[_extents1 select 0, (_extents1 select 1), _extents1 select 2],[_extents2 select 0, (_extents2 select 1)+0.1, _extents2 select 2]];
                    } else {
                        _new pushBack [[(_extents1 select 0), _extents1 select 1, _extents1 select 2],[(_extents2 select 0)+0.1, _extents2 select 1, _extents2 select 2]];
                    };


                } else {
                    _new pushBack _x;
                };
            } forEach _extents;
            missionNamespace setVariable [EPOCH_selectedBuildClass,_new];

            _handled = true;
        };
        case 0xCD     : {
            hint "RIGHT";
            _extents = missionNamespace getVariable [EPOCH_selectedBuildClass,[[[0,0,0],[0,0,0]]]];

            _new = [];
            {
                if (_foreachindex == EPOCH_selectedEditIndex) then {
                    _extents1 = _x select 0;
                    _extents2 = _x select 1;

                    if (_ctrl) then {
                        _new pushBack [[_extents1 select 0, (_extents1 select 1), _extents1 select 2],[_extents2 select 0, (_extents2 select 1)-0.1, _extents2 select 2]];
                    } else {
                        _new pushBack [[(_extents1 select 0), _extents1 select 1, _extents1 select 2],[(_extents2 select 0)-0.1, _extents2 select 1, _extents2 select 2]];
                    };


                } else {
                    _new pushBack _x;
                };
            } forEach _extents;
            missionNamespace setVariable [EPOCH_selectedBuildClass,_new];

            _handled = true;
        };
        case 0xC9 : {
            Epoch_selectedBuildClasIndex = ((Epoch_selectedBuildClasIndex + 1) min EPOCH_buildClassesCount) max 0;
            EPOCH_selectedBuildClass = EPOCH_buildClasses select Epoch_selectedBuildClasIndex;

            if !(isNull EPOCH_currentBuildObj) then {
                deleteVehicle EPOCH_currentBuildObj;
                EPOCH_currentBuildObj = EPOCH_selectedBuildClass createVehicle EPOCH_objPOS;
                EPOCH_currentBuildObj setPosATL EPOCH_objPOS;
                comment "EPOCH_currentBuildObj attachTo[player];";



            };

            hint EPOCH_selectedBuildClass;
            _handled = true;
        };
        case 0xD1 : {
            Epoch_selectedBuildClasIndex = ((Epoch_selectedBuildClasIndex - 1) min EPOCH_buildClassesCount) max 0;
            EPOCH_selectedBuildClass = EPOCH_buildClasses select Epoch_selectedBuildClasIndex;
            hint EPOCH_selectedBuildClass;

            if !(isNull EPOCH_currentBuildObj) then {
                deleteVehicle EPOCH_currentBuildObj;
                EPOCH_currentBuildObj = EPOCH_selectedBuildClass createVehicle EPOCH_objPOS;
                EPOCH_currentBuildObj setPosATL EPOCH_objPOS;
                comment "EPOCH_currentBuildObj attachTo[player];";


            };

            _handled = true;
        };
    };
    _handled
};

[] spawn {
    (findDisplay 46) displayRemoveAllEventHandlers "KeyDown";
    (findDisplay 46) displayAddEventHandler["KeyDown",{_this call EPOCH_KeyDownFunctionhandler}];
};

onEachFrame {

    _numberOfContacts = 0;
    _maxWidth = 0;
    _maxLength = 0;
    _maxHeight = 0;

    _getSettings = missionNamespace getVariable format["%1_MAX",(typeOf EPOCH_currentBuildObj)];
    if !(isNil "_getSettings") then {
        _maxWidth = _getSettings param [0,0];
        _maxLength = _getSettings param [1,0];
        _maxHeight = _getSettings param [2,0];

    } else {
        _bbr = boundingBoxReal EPOCH_currentBuildObj;
        _p1 = _bbr select 0;
        _p2 = _bbr select 1;
        _maxWidth = abs ((_p2 select 0) - (_p1 select 0)/2);
        _maxLength = abs ((_p2 select 1) - (_p1 select 1)/2);
        _maxHeight = abs ((_p2 select 2) - (_p1 select 2));
        missionNamespace setVariable [format["%1_MAX",(typeOf EPOCH_currentBuildObj)],[_maxWidth,_maxLength,_maxHeight]];
    };

    _color = [0,1,0,1];
    {
        _pos1 = EPOCH_currentBuildObj modelToWorld (_x select 0);
        _pos2 = EPOCH_currentBuildObj modelToWorld (_x select 1);

        _ins = lineIntersectsSurfaces [AGLToASL _pos2, AGLToASL _pos1,player,objNull,true,1,"VIEW","FIRE"];
        (_ins select 0) params ["_intersectPosASL","_surfaceNormal","_intersectObject","_parentObject"];

        if (_intersectObject isEqualTo EPOCH_currentBuildObj) then {
        	drawIcon3D ["a3\weapons_f\data\clear_empty.paa", [1,1,1,1], _pos1, 1, 1, 45, "start", 1, 0.02, "TahomaB"];
            drawIcon3D ["a3\weapons_f\data\clear_empty.paa", [1,1,1,1], _pos2, 1, 1, 45, "stop", 1, 0.02, "TahomaB"];
        } else {
            drawIcon3D ["a3\weapons_f\data\clear_empty.paa", [1,1,1,1], ASLToATL (_ins select 0 select 0), 1, 1, 45, "no", 1, 0.02, "TahomaB"];
        };

        drawLine3D [_pos1,_pos2,_color];
    } forEach (missionNamespace getVariable format["%1",(typeOf EPOCH_currentBuildObj)]);

};