package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.datacomponent.MetaDataHandler;
import com.caliverse.admin.domain.response.UsersResponse;
import com.caliverse.admin.dynamodb.domain.atrrib.BlockUserAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.FriendAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.ItemAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.MyHomeAttrib;
import com.caliverse.admin.dynamodb.domain.doc.BlockUserDoc;
import com.caliverse.admin.dynamodb.domain.doc.FriendDoc;
import com.caliverse.admin.dynamodb.domain.doc.ItemDoc;
import com.caliverse.admin.dynamodb.domain.doc.MyhomeDoc;
import com.caliverse.admin.dynamodb.dto.PageResult;
import com.caliverse.admin.dynamodb.repository.BlockUserRepository;
import com.caliverse.admin.dynamodb.repository.FriendRepository;
import com.caliverse.admin.dynamodb.repository.ItemRepository;
import com.caliverse.admin.dynamodb.repository.MyHomeRepository;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
@RequiredArgsConstructor
public class DynamodbFriendService {
    private final FriendRepository friendRepository;
    private final BlockUserRepository blockUserRepository;
    private final DynamodbUserService dynamodbUserService;
    private final MetaDataHandler metaDataHandler;

    public List<UsersResponse.Friend> getFriend(String guid){
        List<UsersResponse.Friend> resList = new ArrayList<>();
        AtomicInteger idx = new AtomicInteger(1);

        Map<String, AttributeValue> pageKey = null;
        do {
            PageResult<FriendDoc> friendDocList = friendRepository.getFriendListWithPaging(guid, "", DynamoDBConstants.ATTRIB_FRIEND, null,"",  pageKey, false);

            friendDocList.getItems().forEach(doc -> {
                UsersResponse.Friend friend = new UsersResponse.Friend();

                FriendAttrib attrib = CommonUtils.stringByObject(doc.getAttribValue(), FriendAttrib.class);

                friend.setRowNum(idx.getAndIncrement());
                String friend_guid = attrib.getFriendGuid();
                friend.setFriendGuid(friend_guid);
                friend.setFriendName(dynamodbUserService.getGuidByName(friend_guid));
                friend.setReceiveDt(attrib.getCreateTime());
                friend.setLanguage(dynamodbUserService.getUserLanguage(friend_guid));
                resList.add(friend);
            });

            pageKey = friendDocList.getLastEvaluatedKey();
        } while (pageKey != null);

        return resList;
    }

    public List<UsersResponse.Friend> getBlockUser(String guid){
        List<UsersResponse.Friend> resList = new ArrayList<>();
        AtomicInteger idx = new AtomicInteger(1);

        Map<String, AttributeValue> pageKey = null;
        do {
            PageResult<BlockUserDoc> friendDocList = blockUserRepository.getBlockUserListWithPaging(guid, "", DynamoDBConstants.ATTRIB_BLOCK_USER, null,"",  pageKey, false);

            friendDocList.getItems().forEach(doc -> {
                UsersResponse.Friend friend = new UsersResponse.Friend();

                BlockUserAttrib attrib = CommonUtils.stringByObject(doc.getAttribValue(), BlockUserAttrib.class);

                friend.setRowNum(idx.getAndIncrement());
                String block_guid = attrib.getBlockGuid();
                friend.setFriendGuid(block_guid);
                friend.setFriendName(dynamodbUserService.getGuidByName(block_guid));
                friend.setReceiveDt(doc.getCreatedDateTime());
                friend.setLanguage(dynamodbUserService.getUserLanguage(block_guid));

                resList.add(friend);
            });

            pageKey = friendDocList.getLastEvaluatedKey();
        } while (pageKey != null);

        return resList;
    }

}
