package com.cirle.scientific.service.impl;

import com.cirle.scientific.dao.QkjCatalogsDao;
import com.cirle.scientific.dao.QkjCatalogsDetailDao;
import com.cirle.scientific.dao.QkjCourseWareDao;
import com.cirle.scientific.dao.QkjTqcourseDao;
import com.cirle.scientific.po.QkjCatalogs;
import com.cirle.scientific.po.QkjCatalogsDetail;
import com.cirle.scientific.po.QkjCourseWare;
import com.cirle.scientific.po.QkjTqcourse;
import com.cirle.scientific.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

@Component
@Transactional
@Slf4j
public class CommonServiceImpl implements CommonService {

    @Autowired
    private QkjCatalogsDao qkjCatalogsDao;

    @Autowired
    private QkjTqcourseDao qkjTqcourseDao;

    @Autowired
    QkjCourseWareDao qkjCourseWareDao;

    @Autowired
    private QkjCatalogsDetailDao qkjCatalogsDetailDao; //select * from qkj_catalogs_detail where obj_type = 2;

    @Override
    public List<QkjCatalogs> getCatalogsByFuId(Integer fuId, Integer type) {
        List<QkjCatalogs> list = qkjCatalogsDao.findByFuId(fuId);
        Optional<QkjCatalogs> byId = qkjCatalogsDao.findById(fuId.longValue());
        if (byId != null && byId.isPresent()) {
            QkjCatalogs qkjCatalogs = byId.get();
            if (qkjCatalogs != null && qkjCatalogs.getFuId() == 0) {
                QkjCatalogs quanbu = new QkjCatalogs();
                quanbu.setId(-1L);
                quanbu.setName("全部");
                list.add(0, quanbu);
            }
        }
        return list;
    }


    private boolean hasCourse(QkjCatalogs qkjCatalogs, List<QkjCatalogsDetail> catalogsDetails, List<QkjCatalogs> all) {
        if (catalogsDetails.stream().map(QkjCatalogsDetail::getCatalogsId).collect(Collectors.toList()).contains(qkjCatalogs.getId().intValue())) { //hasCourse == 1
            List<QkjTqcourse> qkjTqcourses = qkjTqcourseDao.getTqByCataIds(qkjCatalogs.getId().intValue());
            if (qkjTqcourses == null || qkjTqcourses.isEmpty()) return false;
            for (QkjTqcourse qkjTqcours : qkjTqcourses) {
                if (qkjTqcours.getStatus() != 1 || qkjTqcours.getIsVisiable() != 1) {
                    return false;
                }
            }
            return true;
        }
        if (all.stream().map(QkjCatalogs::getFuId).collect(Collectors.toList()).contains(qkjCatalogs.getId().intValue())) { //hasChildren == 1
            List<QkjCatalogs> children = qkjCatalogsDao.findByFuId(qkjCatalogs.getId().intValue());
            for (QkjCatalogs child : children) {
                return hasCourse(child, catalogsDetails, all);
            }
        }
        return false;
    }

    @Override
    public String buildImgUrl(String imgUrl) {
        if (StringUtils.isNotBlank(imgUrl)) {
            if (imgUrl.startsWith("http") || imgUrl.startsWith("https")) {
                return imgUrl;
            } else {
                return "http://files.qkyxzx.com/" + imgUrl;
            }
        }
        return "";
    }

    @Override
    public List<QkjCatalogs> getCatalogsByFuIdForThreeLevel(Integer fuId, Integer type) {
        List<QkjCatalogs> results = new ArrayList<>();

        List<QkjCatalogs> list = qkjCatalogsDao.findByFuId(fuId);
        List<QkjCatalogsDetail> catalogsDetails = qkjCatalogsDetailDao.findByObjType(2);
        List<QkjCatalogs> all = qkjCatalogsDao.findAll();

        for (QkjCatalogs qkjCatalogs : list) {
            if (hasCourse(qkjCatalogs, catalogsDetails, all)) {
                results.add(qkjCatalogs);
            }
        }

        Optional<QkjCatalogs> byId = qkjCatalogsDao.findById(fuId.longValue());
        if (byId != null && byId.isPresent()) {
            QkjCatalogs qkjCatalogs = byId.get();
            if (qkjCatalogs != null && qkjCatalogs.getFuId() == 0) {
                QkjCatalogs quanbu = new QkjCatalogs();
                quanbu.setId(-1L);
                quanbu.setName("全部");
                results.add(0, quanbu);
            }
        }

        return results;
    }

    @Override
    public List<QkjCatalogs> getCatalogsByFuIdForCourseware(Integer fuId, Integer type) {
        List<QkjCatalogs> results = new ArrayList<>();

        List<QkjCatalogs> list = qkjCatalogsDao.findByFuId(fuId);
        List<QkjCatalogsDetail> catalogsDetails = qkjCatalogsDetailDao.findByObjType(5);
        List<QkjCatalogs> all = qkjCatalogsDao.findAll();

        for (QkjCatalogs qkjCatalogs : list) {
            if (hasCourseware(qkjCatalogs, catalogsDetails, all)) {
                results.add(qkjCatalogs);
            }
        }

        Optional<QkjCatalogs> byId = qkjCatalogsDao.findById(fuId.longValue());
        if (byId.isPresent()) {
            QkjCatalogs qkjCatalogs = byId.get();
            if (qkjCatalogs.getFuId() == 0) {
                QkjCatalogs quanbu = new QkjCatalogs();
                quanbu.setId(-1L);
                quanbu.setName("全部");
                results.add(0, quanbu);
            }
        }

        return results;
    }

    private boolean hasCourseware(QkjCatalogs qkjCatalogs, List<QkjCatalogsDetail> catalogsDetails, List<QkjCatalogs> all) {
        if (catalogsDetails.stream().map(QkjCatalogsDetail::getCatalogsId).collect(Collectors.toList()).contains(qkjCatalogs.getId().intValue())) { //hasCourse == 1
            List<QkjCourseWare> qkjCourseWares = qkjCourseWareDao.getByCataIds(qkjCatalogs.getId().intValue());
            if (qkjCourseWares == null || qkjCourseWares.isEmpty()) return false;
            for (QkjCourseWare qkjCourseWare : qkjCourseWares) {
                if (qkjCourseWare.getStatus() != 1 || qkjCourseWare.getIsVisiable() != 1) {
                    return false;
                }
            }
            return true;
        }
        if (all.stream().map(QkjCatalogs::getFuId).collect(Collectors.toList()).contains(qkjCatalogs.getId().intValue())) { //hasChildren == 1
            List<QkjCatalogs> children = qkjCatalogsDao.findByFuId(qkjCatalogs.getId().intValue());
            for (QkjCatalogs child : children) {
                return hasCourseware(child, catalogsDetails, all);
            }
        }
        return false;
    }

}
