package com.yfd.platform.modules.storage.convert.impl;

import com.yfd.platform.modules.config.model.request.SaveStorageSourceRequest;
import com.yfd.platform.modules.readme.model.entity.ReadmeConfig;
import com.yfd.platform.modules.storage.convert.StorageSourceConvert;
import com.yfd.platform.modules.storage.model.dto.StorageSourceAllParamDTO;
import com.yfd.platform.modules.storage.model.dto.StorageSourceDTO;
import com.yfd.platform.modules.storage.model.entity.StorageSource;
import com.yfd.platform.modules.storage.model.result.StorageSourceAdminResult;
import com.yfd.platform.modules.storage.model.result.StorageSourceConfigResult;
import com.yfd.platform.modules.storage.model.result.StorageSourceResult;
import org.springframework.stereotype.Service;

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

/**
 * @Date: 2025/1/10 12:44
 * @Description:
 */
@Service
public class StorageSourceConvertImpl implements StorageSourceConvert {

    @Override
    public List<StorageSourceResult> entityToResultList(List<StorageSource> list) {
        if ( list == null ) {
            return null;
        }

        List<StorageSourceResult> list1 = new ArrayList<StorageSourceResult>( list.size() );
        for ( StorageSource storageSource : list ) {
            list1.add( storageSourceToStorageSourceResult( storageSource ) );
        }

        return list1;
    }

    @Override
    public StorageSourceConfigResult entityToConfigResult(StorageSource storageSource, ReadmeConfig readmeConfig) {
        if ( storageSource == null && readmeConfig == null ) {
            return null;
        }

        StorageSourceConfigResult storageSourceConfigResult = new StorageSourceConfigResult();

        if ( storageSource != null ) {
            storageSourceConfigResult.setEnableFileOperator( storageSource.getEnableFileOperator() );
            storageSourceConfigResult.setDefaultSwitchToImgMode( storageSource.getDefaultSwitchToImgMode() );
        }
        if ( readmeConfig != null ) {
            storageSourceConfigResult.setReadmeDisplayMode( readmeConfig.getDisplayMode() );
            storageSourceConfigResult.setReadmeText( readmeConfig.getReadmeText() );
        }

        return storageSourceConfigResult;
    }

    @Override
    public List<StorageSourceAdminResult> entityToAdminResultList(List<StorageSource> list) {
        if ( list == null ) {
            return null;
        }

        List<StorageSourceAdminResult> list1 = new ArrayList<StorageSourceAdminResult>( list.size() );
        for ( StorageSource storageSource : list ) {
            list1.add( storageSourceToStorageSourceAdminResult( storageSource ) );
        }

        return list1;
    }

    @Override
    public StorageSourceDTO entityToDTO(StorageSource storageSource, StorageSourceAllParamDTO storageSourceAllParam) {
        if ( storageSource == null && storageSourceAllParam == null ) {
            return null;
        }

        StorageSourceDTO storageSourceDTO = new StorageSourceDTO();

        if ( storageSource != null ) {
            storageSourceDTO.setId( storageSource.getId() );
            storageSourceDTO.setName( storageSource.getName() );
            storageSourceDTO.setKey( storageSource.getKey() );
            storageSourceDTO.setRemark( storageSource.getRemark() );
            storageSourceDTO.setType( storageSource.getType() );
            if ( storageSource.getEnable() != null ) {
                storageSourceDTO.setEnable( storageSource.getEnable() );
            }
            storageSourceDTO.setEnableFileOperator( storageSource.getEnableFileOperator() );
            storageSourceDTO.setEnableFileAnnoOperator( storageSource.getEnableFileAnnoOperator() );
            if ( storageSource.getEnableCache() != null ) {
                storageSourceDTO.setEnableCache( storageSource.getEnableCache() );
            }
            if ( storageSource.getAutoRefreshCache() != null ) {
                storageSourceDTO.setAutoRefreshCache( storageSource.getAutoRefreshCache() );
            }
            if ( storageSource.getSearchEnable() != null ) {
                storageSourceDTO.setSearchEnable( storageSource.getSearchEnable() );
            }
            if ( storageSource.getSearchIgnoreCase() != null ) {
                storageSourceDTO.setSearchIgnoreCase( storageSource.getSearchIgnoreCase() );
            }
            storageSourceDTO.setOrderNum( storageSource.getOrderNum() );
            if ( storageSource.getDefaultSwitchToImgMode() != null ) {
                storageSourceDTO.setDefaultSwitchToImgMode( storageSource.getDefaultSwitchToImgMode() );
            }
            storageSourceDTO.setCompatibilityReadme( storageSource.getCompatibilityReadme() );
        }
        storageSourceDTO.setStorageSourceAllParam( storageSourceAllParam );

        return storageSourceDTO;
    }

    @Override
    public StorageSource saveRequestToEntity(SaveStorageSourceRequest saveStorageSourceRequest) {
        if ( saveStorageSourceRequest == null ) {
            return null;
        }

        StorageSource storageSource = new StorageSource();

        storageSource.setId( saveStorageSourceRequest.getId() );
        storageSource.setEnable( saveStorageSourceRequest.getEnable() );
        storageSource.setEnableFileOperator( saveStorageSourceRequest.getEnableFileOperator() );
        storageSource.setEnableFileAnnoOperator( saveStorageSourceRequest.getEnableFileAnnoOperator() );
        storageSource.setEnableCache( saveStorageSourceRequest.isEnableCache() );
        storageSource.setName( saveStorageSourceRequest.getName() );
        storageSource.setKey( saveStorageSourceRequest.getKey() );
        storageSource.setRemark( saveStorageSourceRequest.getRemark() );
        storageSource.setAutoRefreshCache( saveStorageSourceRequest.isAutoRefreshCache() );
        storageSource.setType( saveStorageSourceRequest.getType() );
        storageSource.setSearchEnable( saveStorageSourceRequest.isSearchEnable() );
        storageSource.setSearchIgnoreCase( saveStorageSourceRequest.isSearchIgnoreCase() );
        storageSource.setSearchMode( saveStorageSourceRequest.getSearchMode() );
        storageSource.setOrderNum( saveStorageSourceRequest.getOrderNum() );
        storageSource.setDefaultSwitchToImgMode( saveStorageSourceRequest.isDefaultSwitchToImgMode() );
        storageSource.setCompatibilityReadme( saveStorageSourceRequest.isCompatibilityReadme() );

        return storageSource;
    }

    protected StorageSourceResult storageSourceToStorageSourceResult(StorageSource storageSource) {
        if ( storageSource == null ) {
            return null;
        }

        StorageSourceResult storageSourceResult = new StorageSourceResult();

        storageSourceResult.setName( storageSource.getName() );
        storageSourceResult.setKey( storageSource.getKey() );
        storageSourceResult.setType( storageSource.getType() );
        storageSourceResult.setSearchEnable( storageSource.getSearchEnable() );
        storageSourceResult.setDefaultSwitchToImgMode( storageSource.getDefaultSwitchToImgMode() );

        return storageSourceResult;
    }

    protected StorageSourceAdminResult storageSourceToStorageSourceAdminResult(StorageSource storageSource) {
        if ( storageSource == null ) {
            return null;
        }

        StorageSourceAdminResult storageSourceAdminResult = new StorageSourceAdminResult();

        storageSourceAdminResult.setId( storageSource.getId() );
        storageSourceAdminResult.setEnable( storageSource.getEnable() );
        storageSourceAdminResult.setEnableFileOperator( storageSource.getEnableFileOperator() );
        storageSourceAdminResult.setEnableFileAnnoOperator( storageSource.getEnableFileAnnoOperator() );
        storageSourceAdminResult.setEnableCache( storageSource.getEnableCache() );
        storageSourceAdminResult.setName( storageSource.getName() );
        storageSourceAdminResult.setKey( storageSource.getKey() );
        storageSourceAdminResult.setRemark( storageSource.getRemark() );
        storageSourceAdminResult.setAutoRefreshCache( storageSource.getAutoRefreshCache() );
        storageSourceAdminResult.setType( storageSource.getType() );
        storageSourceAdminResult.setSearchEnable( storageSource.getSearchEnable() );
        storageSourceAdminResult.setSearchIgnoreCase( storageSource.getSearchIgnoreCase() );
        storageSourceAdminResult.setSearchMode( storageSource.getSearchMode() );
        storageSourceAdminResult.setOrderNum( storageSource.getOrderNum() );
        storageSourceAdminResult.setDefaultSwitchToImgMode( storageSource.getDefaultSwitchToImgMode() );
        storageSourceAdminResult.setCompatibilityReadme( storageSource.getCompatibilityReadme() );

        return storageSourceAdminResult;
    }
}
