package com.caliverse.admin.dynamodb.service;

import com.caliverse.admin.domain.datacomponent.MetaDataHandler;
import com.caliverse.admin.domain.entity.CLOTHSMALLTYPE;
import com.caliverse.admin.domain.entity.EFilterOperator;
import com.caliverse.admin.domain.entity.ITEMLARGETYPE;
import com.caliverse.admin.domain.response.UsersResponse;
import com.caliverse.admin.dynamodb.domain.atrrib.ItemAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.MyHomeAttrib;
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.entity.KeyParam;
import com.caliverse.admin.dynamodb.repository.ItemRepository;
import com.caliverse.admin.dynamodb.repository.MyHomeRepository;
import com.caliverse.admin.global.common.annotation.DynamoDBTransaction;
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.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class DynamodbMyHomeService {
    private final MyHomeRepository myHomeRepository;
    private final ItemRepository itemRepository;
    private final MetaDataHandler metaDataHandler;

    public List<UsersResponse.Myhome> getMyHome(String guid){
        List<UsersResponse.Myhome> myhomeList = new ArrayList<>();

        Map<String, AttributeValue> pageKey = null;
        do {
            PageResult<MyhomeDoc> myHomeDocList = myHomeRepository.getMyHomeListWithPaging(guid, "", DynamoDBConstants.ATTRIB_MY_HOME, null,"",  pageKey, false);

            myHomeDocList.getItems().forEach(doc -> {
                UsersResponse.Myhome myhome = new UsersResponse.Myhome();

                MyHomeAttrib attrib = CommonUtils.stringByObject(doc.getAttribValue(), MyHomeAttrib.class);
                String myhome_guid = attrib.getMyhomeGuid();

                myhome.setMyhomeGuid(myhome_guid);
                myhome.setMyhomeName(attrib.getMyhomeName());

                List<UsersResponse.Item> myHomeItemList = new ArrayList<>();
                Map<String, AttributeValue> itemPageKey = null;
                do {
                    PageResult<ItemDoc> itemList = itemRepository.getItemListWithPaging(myhome_guid, "", DynamoDBConstants.ATTRIB_ITEM, null,"",  itemPageKey, false);

                    itemList.getItems().forEach(itemDoc -> {
                        ItemAttrib itemAttrib = CommonUtils.stringByObject(itemDoc.getAttribValue(), ItemAttrib.class);
                        int item_id = itemAttrib.getItemMetaId();
                        String item_name = metaDataHandler.getTextStringData(metaDataHandler.getMetaItemNameData(item_id));

                        UsersResponse.Item item = UsersResponse.Item.builder()
                                .itemId(item_id)
                                .itemName(item_name)
                                .count(itemAttrib.getItemStackCount())
                                .itemGuid(itemAttrib.getItemGuid())
                                .build();
                        myHomeItemList.add(item);

                    });

                    itemPageKey = itemList.getLastEvaluatedKey();
                } while (itemPageKey != null);
                myhome.setPropList(myHomeItemList);
                myhomeList.add(myhome);
            });

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

        return myhomeList;
    }

}
