import { GridCellPlacementRule } from "../../../src/generation/placement/GridCellPlacementRule.js";
import { matcher_tag_traversable_unoccupied } from "../rules/matcher_tag_traversable_unoccupied.js";
import { CellMatcherNot } from "../../../src/generation/rules/logic/CellMatcherNot.js";
import { GridCellActionPlaceTags } from "../../../src/generation/placement/action/GridCellActionPlaceTags.js";
import { GridTags } from "../grid/GridTags.js";
import { GridCellActionPlaceMarker } from "../../../src/generation/markers/GridCellActionPlaceMarker.js";
import { GridTaskExecuteRuleTimes } from "../../../src/generation/grid/generation/GridTaskExecuteRuleTimes.js";
import { CellMatcherLayerBitMaskTest } from "../../../src/generation/rules/CellMatcherLayerBitMaskTest.js";
import { CellMatcherGridPattern } from "../../../src/generation/rules/cell/CellMatcherGridPattern.js";
import { MirGridLayers } from "../grid/MirGridLayers.js";
import {
    GridCellActionTransformNearbyMarkers
} from "../../../src/generation/placement/GridCellActionTransformNearbyMarkers.js";
import { MarkerNodeMatcherByType } from "../../../src/generation/markers/matcher/MarkerNodeMatcherByType.js";
import {
    MarkerNodeTransformerRecordProperty
} from "../../../src/generation/markers/transform/MarkerNodeTransformerRecordProperty.js";
import { CellFilterLiteralFloat } from "../../../src/generation/filtering/numeric/CellFilterLiteralFloat.js";
import { MirMarkerTypes } from "../../../../generator/MirMarkerTypes.js";
import {
    MarkerNodeTransformerRemoveTag
} from "../../../src/generation/markers/transform/MarkerNodeTransformerRemoveTag.js";
import { MirMarkerTags } from "../../../../generator/MirMarkerTags.js";
import {
    MarkerNodeTransformerSequence
} from "../../../src/generation/markers/transform/MarkerNodeTransformerSequence.js";
import { GridCellActionSequence } from "../../../src/generation/placement/action/util/GridCellActionSequence.js";

const MATCH_STARTING_POINT = CellMatcherLayerBitMaskTest.from(GridTags.StartingPoint, MirGridLayers.Tags);


const pattern = new CellMatcherGridPattern();

pattern.addRule(0, 0, matcher_tag_traversable_unoccupied);
pattern.addRule(0, 0, CellMatcherNot.from(MATCH_STARTING_POINT));
// NEXT TO A BASE
pattern.addRule(0, -1, CellMatcherLayerBitMaskTest.from(GridTags.Base, MirGridLayers.Tags));

const rule = GridCellPlacementRule.from(
    {
        matcher: pattern,
        action: GridCellActionSequence.from([
            GridCellActionPlaceTags.from(GridTags.StartingPoint | GridTags.Occupied, MirGridLayers.Tags),
            GridCellActionPlaceMarker.from({ type: MirMarkerTypes.StartingPoint }),
            // transfer ownership of any base within some distance to the player
            GridCellActionTransformNearbyMarkers.from(
                5,
                MarkerNodeMatcherByType.from(MirMarkerTypes.Base),
                MarkerNodeTransformerSequence.from([
                    MarkerNodeTransformerRecordProperty.from('team', CellFilterLiteralFloat.from(0)),
                    MarkerNodeTransformerRemoveTag.from(MirMarkerTags.Encounter)
                ])
            )
        ])
    }
);

rule.allowRotation = true;

export const mir_generator_place_starting_point = () => GridTaskExecuteRuleTimes.from(rule, 1);
