Need help with kinda complicated skill and states

Hello, everyone! Basically what I want to do is a battle skill that gives state 1 to the user if it's already affected by state 2. But if the user is affected by state 1, then the user gets state 2 as long as there are allies (that are not the user) that have state 1. if no ally has state 1, then state 2 is not applied and the user keeps state 1. Also, at the end of each turn, if there are no allies with state 1 (due to having been killed or because of a skill effect), then all allies lose state 2 and get state 1.

Sorry if this sounds so complicated. I have all of yanfly's plugins in case it helps. Thanks for reading!
idk if you still need this, but with Yanfly's Buff and States Core and Skill core this should be fairly simple.

Put this in your skill's notetag.

Spoiler: Skill Notetag
Code:
<After Eval>
if(user.isStateAffected(2)){
  user.addState(1);
} else if(user.isStateAffected(1)){
  for(var i = 0; i < $gameParty.aliveMembers().length; i++){
    if($gameParty.aliveMembers()[i].isStateAffected(1) && user !== $gameParty.aliveMembers()[i])
      user.addState(2);
  }
}
</After Eval>

And put this in state 2's notetag

Spoiler: State Code
Code:
<Custom Turn Start Effect>
user._stateChecked = false;
</Custom Turn Start Effect>
<Custom Turn End Effect>
var clearedStatusCheck = false;
for(var i = 0; i < $gameParty.aliveMembers().length; i++){
    if($gameParty.aliveMembers()[i].isStateAffected(1) && $gameParty.aliveMembers()[i]._stateChecked == false)
      clearedStatusCheck = true;
}
user._stateChecked = true;
if(clearedStatusCheck) {
    user.addState(1);
    user.removeState(stateId);
}
</Custom Turn End Effect>

This is fairly barebones, as I couldn't tell if you wanted the skill to remove state 1 or 2 when adding the other. If you want it to do so, it's pretty easy to implement. Make sure you replace 1 and 2 with the actual state's id numbers, and let me know if you run into any issues.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-kinda-complicated-skill-and-states.171607/