package com.itsol.quantrivanphong.report.issuescomment.bussiness;

import com.itsol.quantrivanphong.model.IssuesComment;
import com.itsol.quantrivanphong.report.issue.common.WrapperResult;
import com.itsol.quantrivanphong.report.issue.repository.IssueRepository;
import com.itsol.quantrivanphong.report.issuescomment.dto.IssuesCommentDTO;
import com.itsol.quantrivanphong.report.issuescomment.repository.IssuesCommentRepository;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class IssuesCommentBussinessImpl implements IssuesCommentBussiness{
    Logger logger = Logger.getLogger(IssuesCommentBussinessImpl.class);
    @Autowired
    private IssueRepository issueRepository;
    @Autowired
    private IssuesCommentRepository issuesCommentRepository;
    @Override
    public List<IssuesCommentDTO> getListComment(Integer issuesId) {
        return lstDTO(issuesCommentRepository.findIssuesCommentByIssuesId(issuesId));
    }

    @Override
    public WrapperResult createComment(IssuesCommentDTO dto) {
        String views ="";
        int status = 113;
        dto.setUserCreate("phungdung");
        if(dto.getUserCreate()!=null){
            try{
            dto.setCreateDate(new Timestamp(System.currentTimeMillis()));
            IssuesComment issuesComment = issuesCommentRepository.save(Models(dto));
                views="tạo comment thành công!";
            status = 200;
            }catch (Exception e){
                logger.info(" Lỗi Insert Comment "+e.getMessage());
                views="Lỗi Tạo Comment";
            }
        }else{
            views="Vui lòng đăng nhập để thực hiện chức năng này";
        }
        return new WrapperResult(status,views);
    }

    @Override
    public WrapperResult updateComment(IssuesCommentDTO dto) {
        return null;
    }

    @Override
    public WrapperResult deleteComment(IssuesCommentDTO dto) {
        return null;
    }

    @Override
    public IssuesCommentDTO findIssuesCommentById(Integer id) {
        return DTO(issuesCommentRepository.findIssuesCommentById(id));
    }
    public IssuesComment Models(IssuesCommentDTO dto){
        IssuesComment issuesComment = new IssuesComment();
        BeanUtils.copyProperties(dto,issuesComment);
        if(dto.getIssueId()!=null){
        issuesComment.setIssues(issueRepository.findIssuesById(dto.getIssueId()));
        }
        return issuesComment;
    }
    public IssuesCommentDTO DTO(IssuesComment issuesComment){
        IssuesCommentDTO dto = new IssuesCommentDTO();
        BeanUtils.copyProperties(issuesComment,dto);
        return dto;
    }
    public List<IssuesCommentDTO> lstDTO(List<IssuesComment> lst){
        List<IssuesCommentDTO> lstDTO = new ArrayList<IssuesCommentDTO>();
        for (IssuesComment issuesComment:lst) {
            lstDTO.add(DTO(issuesComment));
        }
        return lstDTO;
    }
}
