#include_function "lib\Ikareimu\System\Polarity.dnh"
/******************************************
* Variables                               *
******************************************/

let ChainPolarity = NONE;
let ChainScore = 100;
let ChainCountEx = 0;
let ChainList = [];
let ChainCount = 0;
let imgChain = "lib\Ikareimu\img\chain.png";

let CHAIN_POLARITY = 1;
let CHAIN_COUNT = 2;

/******************************************
* Public Functions                        *
******************************************/

sub ChainingManager_Initialize {
  LoadGraphic(imgChain);
}

// Adds the specified polarity to the chain counter. The x and y
// values are for positioning the 1 CHAIN, 2 CHAIN, etc displays
function Chain_Add(polarity, x, y) {
  SetCommonData("chain", GetCommonDataDefault("chain", [])~[[polarity, x, y]]);
}

// Resets chaining to a blank slate. Should be called before each
// stage starts or possibly before an end boss.
sub Chain_Reset {
  ChainPolarity = NONE;
  ChainScore = 100;
  ChainCountEx = 0;
  ChainList = [];
  ChainCount = 0;
}

function Chain_GetInfo(info){
  if(info == CHAIN_POLARITY){
    return ChainPolarity;
  }
  if(info == CHAIN_COUNT){
    return ChainCount;
  }
}

// 
sub ChainingManager_Run {
  Chain_getList;
  let len = length(ChainList);
  if(len > 0){
    loop(len){
      Chain_changeCount(ChainList[0][0], ChainList[0][1], ChainList[0][2]);
      ChainList = erase(ChainList, 0);
    }
  }
}

/******************************************
* Private Functions                       *
******************************************/

sub Chain_getList {
  if(length(GetCommonDataDefault("chain", [])) > 0){
    ChainList = GetCommonDataDefault("chain", []);
    SetCommonData("chain", []);
  }
}

function Chain_changeCount(polarity, x, y) {
  if(ChainCount == 0){
    ChainCount++;
    ChainPolarity = polarity;
  }
  else if(ChainPolarity == polarity){
    ChainCount++;
  }
  else if(ChainPolarity != polarity){
    ChainCount = 1;
    ChainPolarity = polarity;
    Chain_resetScore;
  }
  if(ChainCount == 3){
    ChainCount = 0;
    Chain_addScore(x, y);
  }
}

function Chain_addScore(x, y) {
  ChainCountEx++;
  AddScore(ChainScore);
  Chain_display(x, y, ChainCountEx);
  if(ChainScore < 25600){
    ChainScore *= 2;
  }
}

sub Chain_resetScore {
  ChainScore = 100;
  ChainCountEx = 0;
}

task Chain_display(x, y, count){
  let obj = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(obj, imgChain);
  ObjEffect_SetRenderState(obj, ALPHA);
  ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_SetLayer(obj, 6);
  ObjEffect_CreateVertex(obj, 4);
  if(count == 1){
    ObjEffect_SetVertexUV(obj, 0, 0, 0);
    ObjEffect_SetVertexUV(obj, 1, 111, 0);
    ObjEffect_SetVertexUV(obj, 2, 111, 26);
    ObjEffect_SetVertexUV(obj, 3, 0, 26);
  }
  if(count == 2){
    ObjEffect_SetVertexUV(obj, 0, 0, 26);
    ObjEffect_SetVertexUV(obj, 1, 111, 26);
    ObjEffect_SetVertexUV(obj, 2, 111, 52);
    ObjEffect_SetVertexUV(obj, 3, 0, 52);
  }
  if(count == 3){
    ObjEffect_SetVertexUV(obj, 0, 0, 52);
    ObjEffect_SetVertexUV(obj, 1, 111, 52);
    ObjEffect_SetVertexUV(obj, 2, 111, 78);
    ObjEffect_SetVertexUV(obj, 3, 0, 78);
  }
  if(count == 4){
    ObjEffect_SetVertexUV(obj, 0, 0, 78);
    ObjEffect_SetVertexUV(obj, 1, 111, 78);
    ObjEffect_SetVertexUV(obj, 2, 111, 104);
    ObjEffect_SetVertexUV(obj, 3, 0, 104);
  }
  if(count == 5){
    ObjEffect_SetVertexUV(obj, 0, 0, 104);
    ObjEffect_SetVertexUV(obj, 1, 111, 104);
    ObjEffect_SetVertexUV(obj, 2, 111, 130);
    ObjEffect_SetVertexUV(obj, 3, 0, 130);
  }
  if(count == 6){
    ObjEffect_SetVertexUV(obj, 0, 0, 130);
    ObjEffect_SetVertexUV(obj, 1, 111, 130);
    ObjEffect_SetVertexUV(obj, 2, 111, 156);
    ObjEffect_SetVertexUV(obj, 3, 0, 156);
  }
  if(count == 7){
    ObjEffect_SetVertexUV(obj, 0, 0, 156);
    ObjEffect_SetVertexUV(obj, 1, 111, 156);
    ObjEffect_SetVertexUV(obj, 2, 111, 182);
    ObjEffect_SetVertexUV(obj, 3, 0, 182);
  }
  if(count == 8){
    ObjEffect_SetVertexUV(obj, 0, 0, 182);
    ObjEffect_SetVertexUV(obj, 1, 111, 182);
    ObjEffect_SetVertexUV(obj, 2, 111, 208);
    ObjEffect_SetVertexUV(obj, 3, 0, 208);
  }
  let displayCount = 0;
  while(!Obj_BeDeleted(obj)){
    if(count > 8){
      if(displayCount%6==0){
        ObjEffect_SetVertexUV(obj, 0, 0, 208);
        ObjEffect_SetVertexUV(obj, 1, 111, 208);
        ObjEffect_SetVertexUV(obj, 2, 111, 234);
        ObjEffect_SetVertexUV(obj, 3, 0, 234);
      }
      if(displayCount%6==3){
        ObjEffect_SetVertexUV(obj, 0, 0, 234);
        ObjEffect_SetVertexUV(obj, 1, 111, 234);
        ObjEffect_SetVertexUV(obj, 2, 111, 260);
        ObjEffect_SetVertexUV(obj, 3, 0, 260);
      }
    }
    ObjEffect_SetVertexXY(obj, 0, x-55.5, y-13-displayCount/2);
    ObjEffect_SetVertexXY(obj, 1, x+55.5, y-13-displayCount/2);
    ObjEffect_SetVertexXY(obj, 2, x+55.5, y+13-displayCount/2);
    ObjEffect_SetVertexXY(obj, 3, x-55.5, y+13-displayCount/2);
    if(displayCount>=40){
      let alph = 255-13*(displayCount-40);
      ObjEffect_SetVertexColor(obj, 0, alph, 255, 255, 255);
      ObjEffect_SetVertexColor(obj, 1, alph, 255, 255, 255);
      ObjEffect_SetVertexColor(obj, 2, alph, 255, 255, 255);
      ObjEffect_SetVertexColor(obj, 3, alph, 255, 255, 255);
    }
    if(displayCount>60){
      Obj_Delete(obj);
      return ;
    }
    displayCount++;
    yield ;
  }
}