package io.pcp.parfait.dxm;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Set;

import io.pcp.parfait.dxm.PcpMmvWriter.Store;

class InstanceDomain implements PcpId, PcpOffset, MmvWritable {
    private final String name;
    private final int id;
    private int offset;
    private final Store<Instance> instanceStore;
    private PcpString shortHelpText;
    private PcpString longHelpText;

    InstanceDomain(String name, int id, IdentifierSourceSet instanceStores) {
        this.name = name;
        this.id = id;
        this.instanceStore = new InstanceStoreV1(instanceStores);
    }

    Instance getInstance(String name) {
    	return instanceStore.byName(name);
    }

    @Override
    public String toString() {
        return name + " (" + id + ") " + instanceStore.all().toString();
    }

    public int getId() {
        return id;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
    
    private int getInstanceCount() {
        return instanceStore.size();
    }

    private int getFirstInstanceOffset() {
        return instanceStore.all().iterator().next().getOffset();
    }

    Collection<Instance> getInstances() {
        return instanceStore.all();
    }

    void setHelpText(PcpString shortHelpText, PcpString longHelpText) {
        this.shortHelpText = shortHelpText;
        this.longHelpText = longHelpText;
        
    }

    @Override
    public void writeToMmv(ByteBuffer byteBuffer) {
        byteBuffer.position(offset);
        writeInstanceDomainSection(byteBuffer);
        for (Instance instance : getInstances()) {
            instance.writeToMmv(byteBuffer);
        }
    }

    private void writeInstanceDomainSection(ByteBuffer dataFileBuffer) {
        dataFileBuffer.putInt(id);
        dataFileBuffer.putInt(getInstanceCount());
        dataFileBuffer.putLong(getFirstInstanceOffset());
        dataFileBuffer.putLong(getStringOffset(shortHelpText));
        dataFileBuffer.putLong(getStringOffset(longHelpText));
    }


    private long getStringOffset(PcpString text) {
        if (text == null) {
            return 0;
        }
        return text.getOffset();
    }

    private class InstanceStoreV1 extends Store<Instance> {
        InstanceStoreV1(IdentifierSourceSet identifierSources) {
            super(identifierSources.instanceSource(name));
        }

        @Override
        protected Instance newInstance(String name, Set<Integer> usedIds) {
            return new InstanceV1(InstanceDomain.this, name, identifierSource.calculateId(name,
                    usedIds));
        }

	}

    static final class InstanceDomainStore extends Store<InstanceDomain> {
        private final IdentifierSourceSet identifierSources;

        InstanceDomainStore(IdentifierSourceSet identifierSources) {
            super(identifierSources.instanceDomainSource());
            this.identifierSources = identifierSources;
        }

        @Override
        protected InstanceDomain newInstance(String name, Set<Integer> usedIds) {
            return new InstanceDomain(name, identifierSource.calculateId(name, usedIds), identifierSources);
        }

    }
}
@@ -606,16 +606,28 @@
 ceStores
+, name, this
 );%0A    %7D
@@ -2266,16 +2266,23 @@
 private 
+static 
 class In
@@ -2317,24 +2317,72 @@
 Instance%3E %7B%0A
+        private InstanceDomain instanceDomain;%0A%0A
         Inst
@@ -2422,32 +2422,76 @@
 dentifierSources
+, String name, InstanceDomain instanceDomain
 ) %7B%0A            
@@ -2533,24 +2533,74 @@
 rce(name));%0A
+            this.instanceDomain = instanceDomain;%0A
         %7D%0A%0A 
@@ -2726,17 +2726,17 @@
 tanceV1(
-I
+i
 nstanceD
@@ -2744,13 +2744,8 @@
 main
-.this
 , na
@@ -2786,28 +2786,8 @@
 ame,
-%0A                   
  use

Make InstanceV1Store a static class inner class