package com.samsung.sample.approval.knox.impl;

import com.samsung.approval.ApprovalService;
import com.samsung.approval.annotation.ApprovalDocument;
import com.samsung.approval.entity.ApprovalStep;
import com.samsung.approval.enums.ApprovalDocStatus;
import com.samsung.authentication.Account;
import com.samsung.core.entity.PagingResult;
import com.samsung.resource.FileManagerService;
import com.samsung.sample.approval.knox.KnoxApprovalSampleService;
import com.samsung.sample.approval.knox.dao.KnoxApprovalSampleDao;
import com.samsung.sample.approval.knox.dto.KnoxApprovalSampleSearchDto;
import com.samsung.sample.approval.knox.entity.KnoxApprovalSampleDocument;
import com.samsung.template.TemplateConstants;
import com.samsung.user.entity.User;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class KnoxApprovalSampleServiceImpl implements KnoxApprovalSampleService {

    final
    KnoxApprovalSampleDao knoxApprovalSampleDao;

    final
    FileManagerService fileManagerService;

    final
    MessageSourceAccessor messageSourceAccessor;

    public KnoxApprovalSampleServiceImpl(KnoxApprovalSampleDao knoxApprovalSampleDao, FileManagerService fileManagerService,MessageSourceAccessor messageSourceAccessor) {
        this.knoxApprovalSampleDao = knoxApprovalSampleDao;
        this.fileManagerService = fileManagerService;
        this.messageSourceAccessor = messageSourceAccessor;
    }

    @Override
    public PagingResult<KnoxApprovalSampleDocument> getSampleApprovalDocumentPagingList(KnoxApprovalSampleSearchDto sampleSearchDto) {

        // 시스템 어드민이 아닌 경우 해당 사용자가 등록한 목록만 확인 가능.
        if(Account.currentUser().isSystemAdminUser()) {
            sampleSearchDto.setSystemAdmin(true);
        } else {
            sampleSearchDto.setSystemAdmin(false);
            sampleSearchDto.setFirstRegrId(Objects.requireNonNull(Account.currentUser()).getUserId());
        }

        return knoxApprovalSampleDao.getSampleApprovalDocumentPagingList(sampleSearchDto);
    }

    @Override
    public KnoxApprovalSampleDocument getSampleApprovalDocument(String docId) {
        return knoxApprovalSampleDao.getSampleApprovalDocument(docId);
    }

    @Override
    @Transactional
    public void insertSampleApprovalDocument(KnoxApprovalSampleDocument sampleApprovalDocument) {
        knoxApprovalSampleDao.insertSampleApprovalDocument(sampleApprovalDocument);
    }
    
    @Override
    @Transactional
    public void modifySampleApprovalDocument(KnoxApprovalSampleDocument sampleApprovalDocument) {
        knoxApprovalSampleDao.modifySampleApprovalDocument(sampleApprovalDocument);
    }
    
    @Override
    @Transactional
    public void deleteSampleApprovalDocument(KnoxApprovalSampleDocument sampleApprovalDocument) {
        knoxApprovalSampleDao.deleteSampleApprovalDocument(sampleApprovalDocument);
    }

    @Override
    public void updateSampleApprovalDocument(String docId, ApprovalDocStatus approvalDocStatus) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("docId", docId);
        condition.put("approvalDocStatus", approvalDocStatus);
        knoxApprovalSampleDao.updateSampleApprovalDocument(condition);
    }
}
