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

public class purizeri : MonoBehaviour
{
 int flg=-1;int count=0;int spacewait=0;int level=0;int score=0;int times=0;int wait=0;
 int px=0;int py=0;int nowmove;int puyo;int kaz;int jotai;
 int[] p=new int[100];int[] kazu=new int[2];
 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;
 float stime4f;float ntime4f;
 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<=10;i++) {obj[i]=GameObject.Find("purizeri-"+i);} // 1-10  big-purizeri
  for (i=1;i<=12;i++) {obj[i+10]=GameObject.Find("purizeri-p-"+i);} // 11-22  purizeri-moto
  for (i=0;i<=2;i++) {obj[i+23]=GameObject.Find("purizeri-miniback-"+i);} //  23-25 miniback-moto
  obj[26]=GameObject.Find("purizeri-bar-tate"); // 26  bar-tate
  obj[27]=GameObject.Find("purizeri-bar-yoko"); // 27  bar-yoko
  obj[28]=GameObject.Find("title"); // 28  title
  obj[29]=GameObject.Find("ya-r-32x32"); // 29  ya
  obj[30]=GameObject.Find("button-space"); // 30  button-space
  obj[31]=GameObject.Find("button-esc"); // 31  button-esc
  obj[32]=GameObject.Find("gameend-mes-1"); // 32  gameend-blue
  obj[33]=GameObject.Find("gameend-mes-4"); // 33  gameend-violet
  obj[34]=GameObject.Find("gameover-mes-1"); // 34  gameover-blue
  obj[35]=GameObject.Find("gameover-mes-4"); // 35  gameover-violet
  for (i=0;i<=11;i++)
  {
   obj[i+100]=GameObject.Find("num-"+i); // 100-111  nums-moto
   for (j=0;j<=8;j++) {obj[j*12+i+112]=Object.Instantiate(obj[i+100]) as GameObject;} // 112-219  nums-copy
  }
  for (i=1;i<=12;i++)
  {
   for (j=0;j<=99;j++) {obj[i*100+j+200]=Object.Instantiate(obj[i+10]) as GameObject;} // 300-1499  purizeri-copy
  }
  for (i=0;i<=2;i++)
  {
   for (j=0;j<=99;j++) {obj[i*100+j+1500]=Object.Instantiate(obj[i+23]) as GameObject;} // 1500-1799  miniback-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;
  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();
   }
  }
  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) {gamestart();}
   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) {level=3;gamestart();}
    }
   }
   if (flg==0) {z=29;zx=-96;zy=98-level*40;objput();}
  }
  if (flg==1)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
// input
   moves=0;nowmove=0;
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) {moves=1;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {moves=2;}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) {moves=3;}
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {moves=4;}
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0) {moves=5;}
   if (moves==1 && px>0) {px=px-1;nowmove=1;}
   if (moves==2 && py>0) {py=py-1;nowmove=1;}
   if (moves==3 && px<8) {px=px+1;nowmove=1;}
   if (moves==4 && py<8) {py=py+1;nowmove=1;}
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    if ((mousey>=561 && mousey<=600) || (mousey>=119 && mousey<=158))
    {
     if (mousex>=450 && mousex<=827) {px=(mousex-450)/42;nowmove=1;}
    }
    if ((mousex>=400 && mousex<=439) || (mousex>=840 && mousex<=879))
    {
     if (mousey>=172 && mousey<=549) {py=(549-mousey)/42;nowmove=1;}
    }
    if (mousex>=886 && mousex<=993 && mousey>=534 && mousey<=565) {moves=5;}
   }
   if (nowmove>0)
   {
    z=26;zx=px*42-168;zy=0;objput();z=27;zx=0;zy=168-py*42;objput();
    pzput();
   }
   if (moves==5) {hantei();}
  }
  if (flg==2)
  {
   ntime4f=Time.time;
   if (ntime4f-stime4f>=2f)
   {
    a=puyo%2;
    for (i=1;i<=10;i++) {z=i;zx=-1000;zy=0;objput();}
    z=a+1;zx=-340;zy=-110;objput();z=a+6;zx=340;zy=-110;objput();
    flg=1;newstage();
   }
  }
  if (flg==1 || flg==2)
  {
// times
   ntimef=Time.time;
   if (ntimef-stimef>=12f && times>0) {bgm1.Play();stimef=Time.time;}
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    times=times-1;if (times<0) {times=0;}
    timeput();stime2f=stime2f+1f;
   }
   if (flg==1 && times<=0)
   {
    for (i=1;i<=22;i++) {z=i;zx=-1000;zy=0;objput();}
    z=3;zx=-340;zy=-110;objput();z=9;zx=340;zy=-110;objput();
    z=11;zx=-340;zy=30;objput();z=17;zx=340;zy=30;objput();
    puyo=0;pzput();
    bgmstop();bgm3.Play();
    z=32;zx=0;zy=0;objput();z=30;zx=-1000;zy=0;objput();
    flg=8;stime4f=Time.time;count=0;
   }
  }
  if (flg==1 || flg==2)
  {
   ntime3f=Time.time;
   if (ntime3f-stime3f>=0.5f)
   {
    puyo=puyo+1;if (puyo>=4) {puyo=0;}
    a=puyo%2;
    if (flg==2) {a=a+2;}
    for (i=1;i<=22;i++) {z=i;zx=-1000;zy=0;objput();}
    z=a+1;zx=-340;zy=-110;objput();z=a+6;zx=340;zy=-110;objput();
    if (puyo==0 || puyo==2) {a=0;}
    if (puyo==1) {a=1;}
    if (puyo==3) {a=2;}
    z=a+11;zx=-340;zy=30;objput();z=a+17;zx=340;zy=30;objput();
    pzput();
    stime3f=stime3f+0.5f;
   }
  }
  if (flg>0)
  {
   if (Input.GetMouseButtonDown(0))
   {
    if (mousex>=904 && mousex<=975 && mousey>=474 && mousey<=507) {titleput();}
   }
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();}
  }
  if (flg==8 || flg==9)
  {
   ntime4f=Time.time;
   if (ntime4f-stime4f>=0.5f)
   {
    stime4f=Time.time;count=count+1;
    if (flg==8) {a=32;} else {a=34;}
    a=a+count%2;
    for (i=32;i<=35;i++) {z=i;zx=-1000;zy=0;objput();}
    z=a;zx=0;zy=0;objput();
    if (count>=12) {titleput();}
   }
  }
 }

 void gamestart()
 {
  alldel();
  score=0;times=120;scoreput();timeput();
  px=4;py=4;z=26;zx=0;zy=0;objput();z=27;objput();
  z=1;zx=-340;zy=-110;objput();z=6;zx=340;zy=-110;objput();
  z=11;zx=-340;zy=30;objput();z=17;zx=340;zy=30;objput();
  z=30;zx=300;zy=180;objput();z=31;zx=300;zy=120;objput();
  newstage();
  stimef=Time.time;stime2f=Time.time;stime3f=Time.time;puyo=0;
  bgmstop();bgm1.Play();
  flg=1;spacewait=2;
 }

 void newstage()
 {
  for (i=172;i<=219;i++) {z=i;zx=-1000;zy=0;objput();}
  zy=0;z=183;zx=-346;objput();z=195;zx=-334;objput();
  z=207;zx=334;objput();z=219;zx=346;objput();
  for (i=0;i<=99;i++) {p[i]=0;}
  ax=Random.Range(2,7);if (ax>6) {ax=6;}
  ay=Random.Range(2,7);if (ay>6) {ay=6;}
  r=Random.Range(3,8);if (r>7) {r=7;}
  kaz=r+level*5;jotai=0;
  for (i=0;i3) {jotai=3;}
    }
   }
   if (i%2==0 || jotai==0 || jotai==2)
   {
    for (j=0;j<2;j++)
    {
     e=1;
     r1=Random.Range(0,10);if (r1>9) {r1=9;}
     r2=Random.Range(0,10);if (r2>9) {r2=9;}
     if (r1>ax && r2<=ay) {e=-1;}
     if (r1<=ax && r2>ay) {e=-1;}
     if (p[r2*10+r1]>0) {e=-1;}
     if (e>0)
     {
      if (jotai==1 || jotai==3) {a=2;} else {a=1;}
      p[r2*10+r1]=a;
      break;
     }
     else {j=0;}
    }
   }
   if (i%2==0 || jotai==0 || jotai==1)
   {
    for (j=0;j<2;j++)
    {
     e=1;
     r1=Random.Range(0,10);if (r1>9) {r1=9;}
     r2=Random.Range(0,10);if (r2>9) {r2=9;}
     if (r1>ax && r2>ay) {e=-1;}
     if (r1<=ax && r2<=ay) {e=-1;}
     if (p[r2*10+r1]>0) {e=-1;}
     if (e>0)
     {
      if (jotai==2 || jotai==3) {a=2;} else {a=1;}
      p[r2*10+r1]=a;
      break;
     }
     else {j=0;}
    }
   }
   if (i%2==1) {jotai=0;}
  }
  pzput();
 }

 void pzput()
 {
  for (ii=0;ii<=99;ii++)
  {
   for (jj=0;jj<=14;jj++) {z=jj*100+ii+300;zx=-1000;zy=0;objput();}
   x=ii%10;y=ii/10;xx=x*42-189;yy=189-y*42;
   a=2;
   if (x<=px && y<=py) {a=1;}
   if (x>px && y>py) {a=1;}
   z=a*100+ii+1500;zx=xx;zy=yy;objput();
   if (p[ii]>0)
   {
    aa=a*6+p[ii]*3-9;
    if (puyo==1) {aa=aa+1;}
    if (puyo==3) {aa=aa+2;}
    z=aa*100+ii+300;zx=xx;zy=yy;objput();
   }
  }
 }

 void hantei()
 {
  kazu[0]=0;kazu[1]=0;
  for (i=0;i<=99;i++)
  {
   x=i%10;y=i/10;
   a=1;
   if (x<=px && y<=py) {a=0;}
   if (x>px && y>py) {a=0;}
   kazu[a]=kazu[a]+p[i];
  }
  for (i=172;i<=219;i++) {z=i;zx=-1000;zy=0;objput();}
  zy=0;
  a1=kazu[0]/10;if (kazu[0]<10) {a1=10;}
  a2=kazu[0]%10;
  z=a1+172;zx=-346;objput();z=a2+184;zx=-334;objput();
  a1=kazu[1]/10;if (kazu[1]<10) {a1=10;}
  a2=kazu[1]%10;
  z=a1+196;zx=334;objput();z=a2+208;zx=346;objput();
  if (kazu[0]==kazu[1])
  {
   score=score+1;scoreput();
   a=puyo%2;
   for (i=1;i<=10;i++) {z=i;zx=-1000;zy=0;objput();}
   z=a+3;zx=-340;zy=-110;objput();z=a+8;zx=340;zy=-110;objput();
   bgm2.Play();
   flg=2;stime4f=Time.time;
  }
  else
  {
   for (i=1;i<=22;i++) {z=i;zx=-1000;zy=0;objput();}
   a1=1;a2=6;
   if (kazu[0]999) {score=999;}
  for (ii=100;ii<=135;ii++) {z=ii;zx=-1000;zy=0;objput();}
  a=score/100;if (score<100) {a=10;}
  z=a+100;zx=310;zy=246;objput();
  a=(score%100)/10;if (score<10) {a=10;}
  z=a+112;zx=322;zy=246;objput();
  a=score%10;
  z=a+124;zx=334;zy=246;objput();
 }

 void timeput()
 {
  if (times<0) {times=0;}
  for (ii=136;ii<=171;ii++) {z=ii;zx=-1000;zy=0;objput();}
  a=times/100;if (times<100) {a=10;}
  z=a+136;zx=310;zy=222;objput();
  a=(times%100)/10;if (times<10) {a=10;}
  z=a+148;zx=322;zy=222;objput();
  a=times%10;
  z=a+160;zx=334;zy=222;objput();
 }

 void titleput()
 {
  alldel();z=28;zx=0;zy=0;objput();
  z=29;zx=-96;zy=98-level*40;objput();
  z=1;zx=-340;zy=-110;objput();z=6;zx=340;zy=-110;objput();
  z=11;zx=-340;zy=30;objput();z=17;zx=340;zy=30;objput();
  bgmstop();spacewait=2;flg=0;count=0;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (900,280,100,30),c1,style2);
  GUI.Label (new Rect (900,350,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<=35;i++) {z=i;objput();}
  for (i=100;i<=219;i++) {z=i;objput();}
  for (i=300;i<=1799;i++) {z=i;objput();}
 }
}