When we look at complex RTSs where a simple left button mouse click can mean twenty or more different things, depending on the game and UI state, the code handling this interaction and assigning the right interaction action to the input can easily end up messy and tightly coupled to everything, creating a ball-of-mud architecture/The Class That Does Everything related to interaction.
How can I deal with this situation?
Here's an example of the complex selection mechanics that I'm looking for:
Single left click to select a friendly unit.
If friendly unit is in a group, the group is selected.
If the group is already selected, select the unit.
Single left click to select an enemy unit.
If friendly unit/group already selected, then issue attack order on unit.
If friendly unit/group already selected and enemy unit is in an enemy group, then issue attack order on enemy group.
Same thing but if target is allied non-controllable, then friendlies move towards target.
How can I handle this complex selection system, taking into account complex states?