import { CellFilterLiteralFloat } from "../../../src/generation/filtering/numeric/CellFilterLiteralFloat.js";
import { GridTaskActionRuleSet } from "../../../src/generation/grid/generation/discrete/GridTaskActionRuleSet.js";
import { GridActionRuleSet } from "../../../src/generation/markers/GridActionRuleSet.js";
import { GridCellActionPlaceMarker } from "../../../src/generation/markers/GridCellActionPlaceMarker.js";
import { MarkerNodeMatcherAny } from "../../../src/generation/markers/matcher/MarkerNodeMatcherAny.js";
import { MarkerNodeMatcherByType } from "../../../src/generation/markers/matcher/MarkerNodeMatcherByType.js";
import { MarkerNodeMatcherContainsTag } from "../../../src/generation/markers/matcher/MarkerNodeMatcherContainsTag.js";
import { GridCellPlacementRule } from "../../../src/generation/placement/GridCellPlacementRule.js";
import {
    CellMatcherContainsMarkerWithinRadius
} from "../../../src/generation/rules/cell/CellMatcherContainsMarkerWithinRadius.js";
import { CellMatcherGridPattern } from "../../../src/generation/rules/cell/CellMatcherGridPattern.js";
import { CellMatcherLayerBitMaskTest } from "../../../src/generation/rules/CellMatcherLayerBitMaskTest.js";
import { CellMatcherAnd } from "../../../src/generation/rules/logic/CellMatcherAnd.js";
import { CellMatcherNot } from "../../../src/generation/rules/logic/CellMatcherNot.js";
import { GridTags } from "../grid/GridTags.js";
import { MirGridLayers } from "../grid/MirGridLayers.js";
import { matcher_tag_traversable_unoccupied } from "../rules/matcher_tag_traversable_unoccupied.js";


const matcher_tag_road = CellMatcherLayerBitMaskTest.from(GridTags.Road, MirGridLayers.Tags);

const matchBend90 = new CellMatcherGridPattern();

matchBend90.addRule(0, 0, CellMatcherAnd.from(
    matcher_tag_traversable_unoccupied,
    CellMatcherNot.from(matcher_tag_road)
));

const TAG_ROAD_DECORATOR = 'Road Decorator';

matchBend90.addRule(0, 0,
    CellMatcherAnd.from(
        CellMatcherAnd.from(
            CellMatcherNot.from(CellMatcherContainsMarkerWithinRadius.from(MarkerNodeMatcherByType.from('Road Connector'), 2)),
            CellMatcherNot.from(CellMatcherContainsMarkerWithinRadius.from(MarkerNodeMatcherContainsTag.from(TAG_ROAD_DECORATOR), 3))
        ),
        CellMatcherNot.from(CellMatcherContainsMarkerWithinRadius.from(MarkerNodeMatcherAny.INSTANCE, 0.5))
    )
);

matchBend90.addRule(-1, 0, matcher_tag_road);
matchBend90.addRule(-2, 0, matcher_tag_road);
matchBend90.addRule(0, -1, matcher_tag_road);
matchBend90.addRule(-1, -1, matcher_tag_road);

const placeMarker = GridCellActionPlaceMarker.from({ type: 'Road Junction Decorator 90' });
placeMarker.addTag(TAG_ROAD_DECORATOR);

const ruleBend90 = GridCellPlacementRule.from({
    matcher: matchBend90,
    action:placeMarker,
    probability: CellFilterLiteralFloat.from(0.1)
});


/**
 *
 * @returns {GridTaskActionRuleSet}
 */
export const mir_generator_place_road_decorators = () => GridTaskActionRuleSet.from({ rules: GridActionRuleSet.from({ rules: [ruleBend90] }) });
