using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sei_xor : MonoBehaviour
{
 int flg=-1;int count=0;int spacewait=0;int level=0;int score=0;int times=0;int wait=0;
 int endless=0;int seisy;int nowget;
 int[] bigseis=new int[5];int[] p=new int[30];
 int i;int j;int k;int z;int zx;int zy;int r;int ee;int a;int aa;int aaa;int x;int y;int r1;int r2;int ax;int ay;
 int xx;int yy;int ii;int jj;int kk;
 int e;int e1;int a1;int a2;int moves;int mousex;int mousey;
 float stimef;float ntimef;float stime2f;float ntime2f;float stime3f;float ntime3f;float xf;float yf;
 string c1;string c2;string c3;string c4;
 string d1;string d2;string d3;string datas;

 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[2000];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  for (i=1;i<=5;i++) {obj[i]=GameObject.Find("sei-b-"+i);} // 1-5 sei-big-parts
  for (i=1;i<=30;i++) {obj[i+5]=GameObject.Find("sei-"+i);} // 6-35 sei-charas
  obj[36]=GameObject.Find("sei-xor-title"); // 36  title
  obj[37]=GameObject.Find("sei-xor-title-t1"); // 37  120sec
  obj[38]=GameObject.Find("sei-xor-title-t2"); // 38  endless
  obj[39]=GameObject.Find("ya-r-32x32"); // 39  ya
  obj[40]=GameObject.Find("gameend-mes-4"); // 40  gameend-orange
  obj[41]=GameObject.Find("gameend-mes-2"); // 41  gameend-green
  for (i=0;i<=9;i++)
  {
   obj[i+100]=GameObject.Find("num-"+i); // 100-109  nums-moto
   for (j=0;j<=4;j++) {obj[j*10+i+110]=Object.Instantiate(obj[i+100]) as GameObject;} // 110-159 nums-copy
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].GetComponent();
  style1=new GUIStyle();style1.fontSize=40;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red;
  d1="10000/01000/00100/00010/00001/11000/10100/10010/10001/01100/";
  d2="01010/01001/00110/00101/00011/11100/11010/11001/10110/10101/";
  d3="10011/01110/01101/01011/00111/11110/11101/11011/10111/01111/";
  datas=d1+d2+d3;
  alldel();z=0;zx=-0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update ()
 {
  Vector3 mpos=Input.mousePosition;
  xf=mpos.x;yf=mpos.y;mousex=(int)xf;mousey=(int)yf;
//  c1="X="+mousex.ToString()+" Y="+mousey.ToString();
  if (flg==-1)
  {
   count=count-1;
   if (Input.GetMouseButtonDown(0)) {count=0;}
   if (Input.GetMouseButtonDown(1)) {count=0;}
   if (Input.GetKeyDown(KeyCode.Space)) {count=0;spacewait=2;}
   if (Input.GetKeyDown(KeyCode.Return)) {count=0;}
   if (count<=0)
   {
    titleput();
    flg=0;spacewait=2;
   }
  }
  if (flg==0)
  {
   aa=0;
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {level=level-1;if (level<0) {level=3;}}
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {level=level+1;if (level>3) {level=0;}}
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0)
   {
    if (level<=2) {gamestart();} else {aa=1;}
   }
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    if (mousex>=490 && mousex<=789)
    {
     if (mousey>=440 && mousey<=479) {level=0;gamestart();}
     if (mousey>=400 && mousey<=439) {level=1;gamestart();}
     if (mousey>=360 && mousey<=399) {level=2;gamestart();}
     if (mousey>=320 && mousey<=359) {aa=1;}
    }
   }
   if (flg==0) {z=39;zx=-96;zy=98-level*40;objput();}
   if (aa==1)
   {
    z=37;zx=-1000;zy=0;objput();z=38;objput();
    if (endless==0) {endless=1;} else {endless=0;}
    z=endless+37;zx=24;zy=-20;objput();
   }
  }
  if (flg==1)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
// input
   aa=0;
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) {aa=1;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {aa=2;}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) {aa=3;}
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {aa=4;}
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    if (mousey>=112 && mousey<=194)
    {
     if (mousex>=398 && mousex<=480) {aa=4;}
     if (mousex>=498 && mousex<=580) {aa=1;}
     if (mousex>=598 && mousex<=680) {aa=2;}
     if (mousex>=698 && mousex<=780) {aa=3;}
    }
   }
   if (aa>0)
   {
    if (aa>=1 && aa<=3) {gets(aa-1);}
    if (aa==4) {seisy=0;flg=2;}
   }
// autonext
   ntime3f=Time.time;
   if (ntime3f-stime3f>=1f*(5-level*2)) {seisy=0;flg=2;}
  }
  if (flg==2)
  {
   seisy=seisy+2;seisput();
   if (seisy>=100)
   {
    if (nowget>0)
    {
     for (i=0;i<=4;i++) {bigseis[i]=0;z=i+1;zx=-1000;zy=0;objput();}
    }
    seisy=0;newseis();stime3f=Time.time;flg=1;
   }
  }
  if (flg==1 || flg==2)
  {
// times
   ntimef=Time.time;
   if (ntimef-stimef>=42f && times>0) {bgm1.Play();stimef=Time.time;}
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f && endless==0)
   {
    times=times-1;if (times<0) {times=0;}
    timeput();stime2f=stime2f+1f;
   }
   if (flg==1 && times<=0)
   {
    z=40;zx=0;zy=0;objput();
    bgmstop();bgm2.Play();
    flg=8;count=0;
   }
  }
  if (flg==8 || flg==9)
  {
   a=flg*2+24;
   count=count+1;
   if (count%50==0) {z=a+1;zx=-1000;zy=0;objput();z=a;zx=0;objput();}
   if (count%50==25) {z=a+1;zx=0;zy=0;objput();z=a;zx=-1000;objput();}
   if (count>=325) {titleput();flg=0;spacewait=2;}
  }
  if (flg>=1)
  {
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();flg=0;spacewait=2;}
   if (Input.GetMouseButtonDown(0))
   {
    if (mousex>=914 && mousex<=984 && mousey>=108 && mousey<=160) {titleput();flg=0;spacewait=2;}
   }
  }
 }

 void gamestart()
 {
  alldel();
  for (i=0;i<=11;i++) {p[i]=0;}
  for (i=0;i<=3;i++) {newseis();}
  wait=500-level*200;score=0;times=120;if (endless==1) {times=999;}
  scoreput();timeput();
  stimef=Time.time;stime2f=Time.time;stime3f=Time.time;
  bgmstop();bgm1.Play();
  flg=1;spacewait=2;
 }

 void newseis()
 {
  for (ii=0;ii<=2;ii++)
  {
   for (jj=0;jj<=2;jj++) {p[ii*3+jj]=p[ii*3+jj+3];}
  }
  p[9]=0;p[10]=0;p[11]=0;
  a1=1;a2=16;
  if (level==2) {a1=6;a2=26;}
  if (level==3) {a1=16;a2=31;}
  for (ii=0;ii<=2;ii++)
  {
   for (jj=0;jj<2;jj++)
   {
    ee=-1;e1=1;
    r=Random.Range(a1,a2);
    for (kk=0;kk<=11;kk++)
    {
     if (p[kk]==r) {e1=0;}
    }
    if (e1==1) {ee=r;break;} else {jj=0;}
   }
   p[ii+9]=ee;
  }
  seisy=0;seisput();
 }

 void seisput()
 {
  for (ii=1;ii<=30;ii++) {z=ii+5;zx=-1000;zy=0;objput();}
  for (ii=0;ii<=3;ii++)
  {
   for (jj=0;jj<=2;jj++)
   {
    if (p[ii*3+jj]>0)
    {
     z=p[ii*3+jj]+5;zx=jj*100-100;zy=ii*100-100-seisy;objput();
    }
   }
  }
 }

 void gets(int n)
 {
  if (p[n]>0)
  {
   for (ii=0;ii<=4;ii++)
   {
    d1=datas.Substring((p[n]-1)*6+ii,1);a=int.Parse(d1);
    bigseis[ii]=(bigseis[ii]+a)%2;
    z=ii+1;zx=-1000;zy=0;objput();
    if (bigseis[ii]==1) {z=ii+1;zx=260;zy=-80;objput();}
   }
   z=p[n]+5;zx=-1000;zy=0;objput();p[n]=0;
   a=0;
   for (ii=0;ii<=4;ii++) {a=a+bigseis[ii];}
   if (a>=5) {score=score+1;scoreput();nowget=1;bgm4.Play();}
   else {nowget=0;bgm3.Play();}
   seisy=0;flg=2;
  }
 }

 void scoreput()
 {
  if (score>999) {score=999;}
  for (ii=100;ii<=129;ii++) {z=ii;zx=-1000;zy=0;objput();}
  z=score/100+100;zx=240;zy=150;objput();
  z=(score%100)/10+110;zx=254;zy=150;objput();
  z=score%10+120;zx=268;zy=150;objput();
 }

 void timeput()
 {
  if (times<0) {times=0;}
  for (ii=130;ii<=159;ii++) {z=ii;zx=-1000;zy=0;objput();}
  z=times/100+130;zx=240;zy=120;objput();
  z=(times%100)/10+140;zx=254;zy=120;objput();
  z=times%10+150;zx=268;zy=120;objput();
 }

 void titleput()
 {
  alldel();z=36;zx=0;zy=0;objput();
  z=39;zx=-96;zy=98-level*40;objput();
  z=37;zx=-1000;zy=0;objput();z=38;objput();
  z=endless+37;zx=24;zy=-20;objput();
  bgmstop();spacewait=2;flg=0;count=0;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (900,200,100,30),c1,style2);
  GUI.Label (new Rect (900,300,100,30),c2,style2);
  GUI.Label (new Rect (330,200,100,30),c3,style1);
  GUI.Label (new Rect (530,300,100,30),c4,style3);
 }

 void objput()
 {
  Vector3 pos=obj[z].transform.position;
  pos.x=zx*0.01f;pos.y=zy*0.01f;
  obj[z].transform.position=pos;
 }

 void bgmstop()
 {
  bgm1.Stop();bgm2.Stop();bgm3.Stop();bgm4.Stop();
 }

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  zx=-1000;zy=0;for (i=1;i<=41;i++) {z=i;objput();}
  for (i=100;i<=159;i++) {z=i;objput();}
 }
}