import 'babel-polyfill';
import express from 'express';
import {
    getGoodsByGroupController,
    getGoodsByIdController,
    getFavouriteGoodsController,
    appendFavouriteGoodsController,
    deleteFavouriteGoodsController,
    appendOrdersController,
    dynamicSearchGoodsController,
    deletePictureController,
    updateGoodsController,
    deleteSingleGoodsController,
    appendPictureForAttributeController,
    updateGoodsAttributesController,
    deleteGoodsAttributesController,
    filterTypesCounterController,
    orderBySearchStringController
} from "./controllers/goodsController";
import {
    getSignUpLocationController,
    signUpController,
    authController,
    checkAuthController,
    getPersonalDataController,
    updatePersonalDataController,
    getUserOrdersController,
    deleteOrderController,
    getAllOrdersController,
    deleteOrdersByAdminController,
    appendOrdersByAdminController,
    updateOrdersByAdminController
} from "./controllers/userController";
import groupTypesSelector from "./middleWares/groupTypesSelector";
import goodsFilter from "./middleWares/goodsFilter";
import hashPassword from './middleWares/hashPassword';
import userCreator from "./middleWares/userCreator";
import signUpValidation from "./middleWares/validation/signUpValidation";
import tokenGenerator from "./middleWares/tokenGenerator";
import checkAuthEmail from "./middleWares/checkAuthEmail";
import checkAuthPassword from './middleWares/checkAuthPassword';
import tokenVerify from "./middleWares/tokenVerify";
import updatePassword from "./middleWares/updatePassword";
import updateDataValidation from './middleWares/validation/updateDataValidation';
import getFavouriteGoodsUserArray from './middleWares/getFavouriteGoodsArray';
import favouritesGoodsCreator from './middleWares/favouritesGoodsCreator';
import favouritesGoodsDeleter from './middleWares/favouritesGoodsDeleter';
import getUserOrders from "./middleWares/getUserOrders";
import ordersDeletor from './middleWares/ordersDeletor';
import registerUserOrNot from './middleWares/registerUserOrNot';
import getUserDataForOrder from './middleWares/getUserDataForOrder';
import ordersCreator from './middleWares/ordersCreator';
import encryptedDataVerify from './middleWares/encryptedDataVerify';
import adminOrdersGetter from './middleWares/adminOrdersGetter';
import ordersUpdator from './middleWares/ordersUpdator';
import pictureDeletor from './middleWares/pictureDeletor';
import goodsUpdator from './middleWares/goodsUpdator';
import goodsDeletor from './middleWares/goodsDeletor';
import goodsAttributesDeletor from './middleWares/goodsAttributesDeletor';
import pictureInAttributeAppender from './middleWares/pictureInAttributeAppender';
import goodsAttributesUpdator from './middleWares/goodsAttributesUpdator';
import orderSearcherByString from './middleWares/orderSearcherByString';


const router = express.Router();

router.get(
    "/group/:id",
    groupTypesSelector,
    goodsFilter,
    getGoodsByGroupController
);
router.get(
    "/product/:id",
    getGoodsByIdController
);
router.get(
    "/signUpLocation",
    getSignUpLocationController
);

router.get(
    `/countFilterTypes/:id`,
    groupTypesSelector,
    filterTypesCounterController
);

router.post(
    '/signUp',
    signUpValidation,
    hashPassword,
    userCreator,
    tokenGenerator,
    signUpController
);
router.post(
    "/auth",
    checkAuthEmail,
    checkAuthPassword,
    tokenGenerator,
    authController
);
router.get(
    "/checkedAuth",
    tokenVerify,
    checkAuthController
);
router.get(
    "/getPersonalData",
    tokenVerify,
    getPersonalDataController
);
router.get(
    "/getFavouriteGoods",
    tokenVerify,
    getFavouriteGoodsUserArray,
    getFavouriteGoodsController
);
router.post(
    "/updatePersonalData",
    updateDataValidation,
    tokenVerify,
    updatePassword,
    checkAuthPassword,
    hashPassword,
    updatePersonalDataController
);

router.get(
    "/appendFavouriteGoods/:id",
    tokenVerify,
    favouritesGoodsCreator,
    getFavouriteGoodsUserArray,
    appendFavouriteGoodsController
);

router.get(
    '/deleteFavouriteGoods/:id',
    tokenVerify,
    favouritesGoodsDeleter,
    getFavouriteGoodsUserArray,
    deleteFavouriteGoodsController
);

router.get(
    "/getUserOrders",
     tokenVerify,
    getUserOrders,
    getUserOrdersController
);

router.get(
  '/deleteOrder/:id',
  tokenVerify,
  ordersDeletor,
  getUserOrders,
  deleteOrderController
);

router.post(
  '/appendOrders',
  registerUserOrNot,
  getUserDataForOrder,
  ordersCreator,
  appendOrdersController
);

router.post(
    '/getAllOrders',
    tokenVerify,
    encryptedDataVerify,
    adminOrdersGetter,
    getAllOrdersController
);

router.get(
  '/getOrdersBySearchString/:searchString',
    tokenVerify,
    encryptedDataVerify,
    orderSearcherByString,
    orderBySearchStringController
);

router.get(
    '/deleteOrderByAdmin/:id',
    tokenVerify,
    encryptedDataVerify,
    ordersDeletor,
    adminOrdersGetter,
    deleteOrdersByAdminController
);

router.get(
  '/dynamicSearchGoods/:searchString',
    dynamicSearchGoodsController
);

router.post(
    '/appendOrderByAdmin',
    tokenVerify,
    encryptedDataVerify,
    ordersCreator,
    adminOrdersGetter,
    appendOrdersByAdminController
);

router.post(
  '/updateOrderByAdmin',
  tokenVerify,
  encryptedDataVerify,
  ordersUpdator,
  adminOrdersGetter,
  updateOrdersByAdminController
);

router.post(
    '/deletePicture',
    tokenVerify,
    encryptedDataVerify,
    pictureDeletor,
    deletePictureController
);

router.post(
    '/updateGoods',
    tokenVerify,
    encryptedDataVerify,
    goodsUpdator,
    updateGoodsController
);

router.get(
  '/deleteSingleGoods/:id',
    tokenVerify,
    encryptedDataVerify,
    goodsDeletor,
    deleteSingleGoodsController
);

router.post(
    '/appendPictureInAttribute',
    tokenVerify,
    encryptedDataVerify,
    pictureInAttributeAppender,
    appendPictureForAttributeController
);

router.post(
  '/updateGoodsAttributes',
    tokenVerify,
    encryptedDataVerify,
    goodsAttributesUpdator,
    updateGoodsAttributesController
);

router.get(
  '/deleteGoodsAttribute/:id',
    tokenVerify,
    encryptedDataVerify,
    goodsAttributesDeletor,
    pictureDeletor,
    deleteGoodsAttributesController
);

module.exports = router;

