package eu.dnetlib.uoaadmintools.controllers;

import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
import eu.dnetlib.uoaadmintools.dao.PageDAO;
import eu.dnetlib.uoaadmintools.entities.Community;
import eu.dnetlib.uoaadmintools.entities.Page;
import eu.dnetlib.uoaadmintools.entities.PageHelpContent;
import eu.dnetlib.uoaadmintools.dao.PageHelpContentDAO;
import eu.dnetlib.uoaadmintools.entities.PageHelpContentResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class PageHelpContentController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private PageHelpContentDAO pageHelpContentDAO;

    @Autowired
    private PageDAO pageDAO;

    @Autowired
    private CommunityDAO communityDAO;

    @RequestMapping(value = "/pagehelpcontent", method = RequestMethod.GET)
    public List<PageHelpContentResponse> getPageHelpContents(@RequestParam(required=false) String community,
                                                             @RequestParam(required=false) String page,
                                                             @RequestParam(required=false) String position,
                                                             @RequestParam(required=false) String active,
                                                             @RequestParam(required=false) String before) {
        List<PageHelpContent> pageHelpContents = null;

        Community _community = null;
        String communityId = null;
        if(community != null) {
            _community = communityDAO.findByPid(community);
            if(_community != null) {
                 communityId = _community.getId();
            }
        }
        //pageHelpContents = pageHelpContentDAO.findByCommunityAndPlacementAndIsActiveAndisPriorToOrderByOrderAsc(community, position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));

        if(community != null && position != null && active != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndPlacementAndIsActiveAndIsPriorToOrderByOrderAsc(communityId, position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
        } else if(community != null && position != null && active != null) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndPlacementAndIsActiveOrderByOrderAsc(communityId, position, Boolean.parseBoolean(active));
        } else if(community != null && position != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndPlacementAndIsPriorToOrderByOrderAsc(communityId, position, Boolean.parseBoolean(before));
        } else if(community != null && active != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndIsActiveAndIsPriorToOrderByPlacementAscOrderAsc(communityId, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
        } else if(position != null && active != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsActiveAndIsPriorToOrderByOrderAsc(position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
        } else if(community != null && position != null ) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndPlacementOrderByOrderAsc(communityId, position);
        } else if(community != null && active != null ) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndIsActiveOrderByPlacementAscOrderAsc(communityId, Boolean.parseBoolean(active));
        } else if(community != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByCommunityAndIsPriorToOrderByPlacementAscOrderAsc(communityId, Boolean.parseBoolean(before));
        } else if(position != null && active != null) {
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsActiveOrderByOrderAsc(position, Boolean.parseBoolean(active));
        } else if(position != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsPriorToOrderByOrderAsc(position,  Boolean.parseBoolean(before));
        } else if(active != null && before != null) {
            pageHelpContents = pageHelpContentDAO.findByIsActiveAndIsPriorToOrderByPlacementAscOrderAsc(Boolean.parseBoolean(active), Boolean.parseBoolean(before));
        } else if(community != null) {
            pageHelpContents = pageHelpContentDAO.findByCommunityOrderByPlacementAscOrderAsc(communityId);
        } else if(position != null) {
            pageHelpContents = pageHelpContentDAO.findByPlacementOrderByOrderAsc(position);
        } else if(active != null) {
            pageHelpContents = pageHelpContentDAO.findByIsActiveOrderByPlacementAscOrderAsc(Boolean.parseBoolean(active));
        } else if(before != null) {
            pageHelpContents = pageHelpContentDAO.findByIsPriorToOrderByPlacementAscOrderAsc(Boolean.parseBoolean(before));
        } else {
            pageHelpContents = pageHelpContentDAO.findAllByOrderByPlacementAscOrderAsc();
        }

        List<PageHelpContentResponse> pageHelpContentResponses = new ArrayList<>();
        for (PageHelpContent pageHelpContent : pageHelpContents) {
            Page _page = pageDAO.findById(pageHelpContent.getPage());
            if(page == null || page.equals(_page.getRoute())) {
                PageHelpContentResponse pageHelpContentResponse = new PageHelpContentResponse(pageHelpContent);

                pageHelpContentResponse.setPage(_page);
                pageHelpContentResponse.setCommunity(communityDAO.findById(pageHelpContent.getCommunity()));
                pageHelpContentResponses.add(pageHelpContentResponse);
            }
        }
        return pageHelpContentResponses;
    }
/*
    @RequestMapping(value = "/pagehelpcontent/community/{id}", method = RequestMethod.GET)
    public List<PageHelpContentResponse> getCommunityPageHelpContents(@PathVariable(value = "id") String communityId) {
        List<PageHelpContent> pageHelpContents =  pageHelpContentDAO.findByCommunity(communityId);
        List<PageHelpContentResponse> pageHelpContentResponses = new ArrayList<>();
        for (PageHelpContent pageHelpContent : pageHelpContents) {
            PageHelpContentResponse pageHelpContentResponse = new PageHelpContentResponse(pageHelpContent);
            pageHelpContentResponse.setPage(pageDAO.findById(pageHelpContent.getPage()));
            pageHelpContentResponse.setCommunity(communityDAO.findById(pageHelpContent.getCommunity()));
            pageHelpContentResponses.add(pageHelpContentResponse);
        }
        return pageHelpContentResponses;
    }
*/
    @RequestMapping(value = "/pagehelpcontent", method = RequestMethod.DELETE)
    public void deleteAllPageHelpContents() {
        pageHelpContentDAO.deleteAll();
    }

    @RequestMapping(value = "/pagehelpcontent/save", method = RequestMethod.POST)
    public PageHelpContent insertPageHelpContent(@RequestBody PageHelpContent pageHelpContent) {
        String communityId = communityDAO.findByPid(pageHelpContent.getCommunity()).getId();
        pageHelpContent.setCommunity(communityId);
        return pageHelpContentDAO.save(pageHelpContent);
    }

    @RequestMapping(value = "/pagehelpcontent/update", method = RequestMethod.POST)
    public PageHelpContent updatePageHelpContent(@RequestBody PageHelpContent pageHelpContent) {
        return pageHelpContentDAO.save(pageHelpContent);
    }

    @RequestMapping(value = "/pagehelpcontent/{id}", method = RequestMethod.GET)
    public PageHelpContent getPageHelpContent(@PathVariable(value = "id") String id) {
        return pageHelpContentDAO.findById(id);
    }

    @RequestMapping(value = "/pagehelpcontent/toggle", method = RequestMethod.POST)
    public List<String> togglePageHelpContent(@RequestBody List<String> pageHelpContents, @RequestParam String status) throws Exception {
        for (String id: pageHelpContents) {
            log.debug("Id of pageHelpContent: "+id);
            PageHelpContent pageHelpContent = pageHelpContentDAO.findById(id);
            pageHelpContent.setIsActive(Boolean.parseBoolean(status));
            pageHelpContentDAO.save(pageHelpContent);
        }
        return pageHelpContents;
    }

    @RequestMapping(value = "/pagehelpcontent/{id}", method = RequestMethod.DELETE)
    public void deletePageHelpContent(@PathVariable(value = "id") String id) {
        pageHelpContentDAO.delete(id);
    }

    @RequestMapping(value = "/pagehelpcontent/delete", method = RequestMethod.POST)
    public Boolean deletePageHelpContents(@RequestBody List<String> pageHelpContents) throws Exception {
        for (String id: pageHelpContents) {
            pageHelpContentDAO.delete(id);
        }
        return true;
    }


}


