package com.alterdekim.game.service;

import com.alterdekim.game.component.game.PlayerProperties;
import com.alterdekim.game.component.game.response.location.LocationObject;
import com.alterdekim.game.entity.Good;
import com.alterdekim.game.entity.HouseLocation;
import com.alterdekim.game.entity.Location;
import com.alterdekim.game.entity.LocationObjectInstance;
import com.alterdekim.game.repository.GoodRepository;
import com.alterdekim.game.repository.HouseLocationRepository;
import com.alterdekim.game.repository.LocationObjectInstanceRepository;
import com.alterdekim.game.repository.LocationRepository;
import com.alterdekim.game.utils.GameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class LocationService {

    @Autowired
    private LocationRepository repository;

    @Autowired
    private LocationObjectInstanceRepository locationObjectInstanceRepository;

    @Autowired
    private HouseLocationRepository houseLocationRepository;

    @Autowired
    private AvatarInventoryService avatarInventoryService;

    @Autowired
    private GoodRepository goodRepository;

    @Autowired
    private UserService userService;

    public List<LocationObjectInstance> getAllLocationObjects() {
        return this.locationObjectInstanceRepository.findAll();
    }

    public LocationObject getDefaultLocation() {
        Optional<Location> l = this.repository.findDefaultLocation();
        if( l.isPresent() ) {
            Location r = l.get();
            return new LocationObject(r.getLocationId(), false, false, false, true, r.getMediaResourceId(), r.getX(), r.getY(), r.getName(), 0, "", this.locationObjectInstanceRepository.findObjectsForLocationById(r.getLocationId()));
        }
        return new LocationObject();
    }

    public LocationObject getLocationById(Long locationId) {
        Optional<Location> l = this.repository.findLocationById(locationId);
        if( l.isPresent() ) {
            Location r = l.get();
            return new LocationObject(r.getLocationId(), false, false, false, true, r.getMediaResourceId(), r.getX(), r.getY(), r.getName(), 0, "", this.locationObjectInstanceRepository.findObjectsForLocationById(locationId));
        }
        return getDefaultLocation();
    }

    public void addLocation(Location location) {
        this.repository.save(location);
    }

    public List<Location> getAll() {
        return repository.findAll();
    }

    public void removeById(Long id) {
        this.repository.deleteById(id);
    }

    public void removeObjectById(Long id) {
        this.locationObjectInstanceRepository.deleteById(id);
    }

    public void addObject(LocationObjectInstance objectInstance) {
        this.locationObjectInstanceRepository.save(objectInstance);
    }


    public LocationObject getHomeByUserId(Integer userId) {
        Long goodId = goodRepository.findById(avatarInventoryService.getUsedHouse(userId)).map(Good::getId).orElse(102L);
        var house = this.houseLocationRepository.findDefaultHouseLocationByGoodId(goodId).get();
        return new LocationObject(GameUtils.convertToSpecialLocation(userId, true), true, false, userService.getBooleanUserProperty(userId, PlayerProperties.IsHomeLocked, true), true, house.getMediaResourceId().longValue(), 300.0, 300.0, "", userId, userService.getUsernameById(userId).get(), Collections.emptyList());
    }

    public void addHouseLocation(HouseLocation house) {
        this.houseLocationRepository.save(house);
    }
}
