package io.cattle.platform.core.dao.impl;

import io.cattle.platform.core.constants.CommonStatesConstants;
import io.cattle.platform.core.dao.InstanceDao;
import io.cattle.platform.core.model.Credential;
import io.cattle.platform.core.model.GenericObject;
import io.cattle.platform.core.model.Instance;
import io.cattle.platform.core.model.tables.records.CredentialRecord;
import io.cattle.platform.core.model.tables.records.InstanceRecord;
import io.cattle.platform.db.jooq.dao.impl.AbstractJooqDao;
import io.cattle.platform.json.JsonMapper;
import io.cattle.platform.object.ObjectManager;
import io.cattle.platform.object.util.DataAccessor;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Configuration;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static io.cattle.platform.core.model.tables.CredentialTable.*;
import static io.cattle.platform.core.model.tables.GenericObjectTable.*;
import static io.cattle.platform.core.model.tables.InstanceTable.*;


public class InstanceDaoImpl extends AbstractJooqDao implements InstanceDao {

    ObjectManager objectManager;
    JsonMapper jsonMapper;

    public InstanceDaoImpl(Configuration configuration, ObjectManager objectManager, JsonMapper jsonMapper) {
        super(configuration);
        this.objectManager = objectManager;
        this.jsonMapper = jsonMapper;
    }

    @Override
    public List<? extends Instance> getNonRemovedInstanceOn(Long hostId) {
        return create()
                .select(INSTANCE.fields())
                .from(INSTANCE)
                .where(INSTANCE.REMOVED.isNull()
                        .and(INSTANCE.HOST_ID.eq(hostId))
                        .and(INSTANCE.STATE.notIn(CommonStatesConstants.ERROR,
                                CommonStatesConstants.ERRORING,
                                CommonStatesConstants.REMOVING)))
                .fetchInto(InstanceRecord.class);
    }

    @Override
    public Instance getInstanceByUuidOrExternalId(Long clusterId, String uuid, String externalId) {
        Instance instance = null;
        Condition condition = INSTANCE.CLUSTER_ID.eq(clusterId);

        if(StringUtils.isNotEmpty(uuid)) {
            instance = create()
                    .selectFrom(INSTANCE)
                    .where(condition
                    .and(INSTANCE.UUID.eq(uuid)))
                    .fetchAny();
        }

        if (instance == null && StringUtils.isNotEmpty(externalId)) {
            instance = create()
                    .selectFrom(INSTANCE)
                    .where(condition
                    .and(INSTANCE.EXTERNAL_ID.eq(externalId)))
                    .fetchAny();
        }

        return instance;
    }

    @Override
    public List<? extends Instance> getOtherDeploymentInstances(Instance instance) {
        if (instance.getDeploymentUnitId() == null) {
            return Collections.emptyList();
        }

        return create().select(INSTANCE.fields())
                .from(INSTANCE)
                .where(INSTANCE.DEPLOYMENT_UNIT_ID.eq(instance.getDeploymentUnitId())
                    .and(INSTANCE.STATE.ne(CommonStatesConstants.REMOVING))
                    .and(INSTANCE.DESIRED.isTrue())
                    .and(INSTANCE.REMOVED.isNull())
                    .and(INSTANCE.ID.ne(instance.getId())))
                .fetchInto(InstanceRecord.class);
    }

    @Override
    public List<? extends Credential> getCredentials(Set<Long> credentialIds) {
        return create().select(CREDENTIAL.fields())
                .from(CREDENTIAL)
                .where(CREDENTIAL.ID.in(credentialIds))
                .fetchInto(CredentialRecord.class);
    }

    protected Map<String, Object> lookupCacheInstanceData(long instanceId) {
        Instance instance = objectManager.loadResource(Instance.class, instanceId);
        if (instance == null) {
            return Collections.emptyMap();
        }
        Map<String, Object> newData = new HashMap<>();
        newData.put(DataAccessor.FIELDS, instance.getData().get(DataAccessor.FIELDS));
        return newData;
    }

    GenericObject getPullTask(long accountId, String image, Map<String, String> labels) {
        List<GenericObject> tasks = objectManager.find(GenericObject.class, GENERIC_OBJECT.ACCOUNT_ID, accountId,
                GENERIC_OBJECT.REMOVED, null);
        for (GenericObject task : tasks) {
            if (getPullTaskUuid(accountId, image, labels)
                    .equalsIgnoreCase(DataAccessor.fieldString(task, "pullTaskUuid"))) {
                return task;
            }
        }
        return null;
    }

    private String getPullTaskUuid(long accountId, String image, Map<String, String> labels) {
        if (labels == null) {
            labels = new HashMap<>();
        }
        try {
            return String.format("%d:%s:%s", accountId, image, jsonMapper.writeValueAsString(labels));
        } catch (IOException e) {
            return String.format("%d:%s:%s", accountId, image);
        }
    }

}

@@ -201,60 +201,8 @@
 al;%0A
-import io.cattle.platform.core.model.GenericObject;%0A
 impo
@@ -244,16 +244,16 @@
 stance;%0A
+
 import i
@@ -537,60 +537,8 @@
 er;%0A
-import io.cattle.platform.object.util.DataAccessor;%0A
 impo
@@ -641,36 +641,8 @@
 n;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -671,34 +671,8 @@
 ns;%0A
-import java.util.HashMap;%0A
 impo
@@ -694,30 +694,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -713,16 +713,16 @@
 l.Set;%0A%0A
+
 import s
@@ -787,81 +787,8 @@
 .*;%0A
-import static io.cattle.platform.core.model.tables.GenericObjectTable.*;%0A
 impo
@@ -3537,1352 +3537,6 @@
  %7D%0A%0A
-    protected Map%3CString, Object%3E lookupCacheInstanceData(long instanceId) %7B%0A        Instance instance = objectManager.loadResource(Instance.class, instanceId);%0A        if (instance == null) %7B%0A            return Collections.emptyMap();%0A        %7D%0A        Map%3CString, Object%3E newData = new HashMap%3C%3E();%0A        newData.put(DataAccessor.FIELDS, instance.getData().get(DataAccessor.FIELDS));%0A        return newData;%0A    %7D%0A%0A    GenericObject getPullTask(long accountId, String image, Map%3CString, String%3E labels) %7B%0A        List%3CGenericObject%3E tasks = objectManager.find(GenericObject.class, GENERIC_OBJECT.ACCOUNT_ID, accountId,%0A                GENERIC_OBJECT.REMOVED, null);%0A        for (GenericObject task : tasks) %7B%0A            if (getPullTaskUuid(accountId, image, labels)%0A                    .equalsIgnoreCase(DataAccessor.fieldString(task, %22pullTaskUuid%22))) %7B%0A                return task;%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A    private String getPullTaskUuid(long accountId, String image, Map%3CString, String%3E labels) %7B%0A        if (labels == null) %7B%0A            labels = new HashMap%3C%3E();%0A        %7D%0A        try %7B%0A            return String.format(%22%25d:%25s:%25s%22, accountId, image, jsonMapper.writeValueAsString(labels));%0A        %7D catch (IOException e) %7B%0A            return String.format(%22%25d:%25s:%25s%22, accountId, image);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Delete unused code