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

public class rainbow_puzzle : MonoBehaviour
{
 int flg=0;int count=0;int spacewait=0;int level=0;
 int anax;int anay;int px;
 float kumoxf=0f;float kumoxxf=0.1f;
 int[] p=new int[63];
 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 ii;int jj;int e1;int e2;int n1;int n2;int ax;int ay;int aax;int aay;int cx;int cy;
 int a1;int a2;int a3;int moves;
 float stimef;float ntimef;
 string c1;string c2;string c3;string c4;
 string d;string d1;string d2;string savedata;string keyname;
 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[704];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  obj[1]=GameObject.Find("rainbow-kumo-1"); // 1  kumo-1
  obj[2]=GameObject.Find("rainbow-kumo-2"); // 2  kumo-2
  obj[3]=GameObject.Find("rainbow-taiyou-3"); // 3  taiyou-lines
  obj[4]=GameObject.Find("rainbow-ya"); // 4 yajirushi
  for (i=1;i<=4;i++)
  {
   obj[i+4]=GameObject.Find("rainbow-r-beta-"+i); // 5-8  rainbow-beta
  }
  obj[9]=GameObject.Find("rainbow-title"); // 9 title
  obj[10]=GameObject.Find("rainbow-to-end"); // 10 to-end
  obj[11]=GameObject.Find("rainbow-clear-1"); // 11 clear-1
  obj[12]=GameObject.Find("rainbow-clear-2"); // 12 clear-2
  for (i=0;i<=7;i++)
  {
   for (j=0;j<=6;j++)
   {
    obj[i*7+j+100]=GameObject.Find("rainbow-"+(j+1)+"-"+i); // 100-155  blocks-moto
   }
  }
  for (i=0;i<=7;i++)
  {
   for (j=0;j<=6;j++)
   {
    for (k=0;k<=8;k++)
    {
     obj[i*63+j*9+k+200]=Object.Instantiate(obj[i*7+j+100]) as GameObject; // 200 - 703
     obj[i*63+j*9+k+200].transform.Rotate(0,0,-22.5f*k+101.25f);
     if (i-1==j) {zx=0;} else {zx=-2000;}
     z=i*63+j*9+k+200;zy=-260;objput();
    }
   }
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();
  style1=new GUIStyle();style1.fontSize=80;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red; 
  c1="";c2="";c3="";c4="";
  z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update ()
 {
  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)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKeyDown(KeyCode.LeftArrow))
   {
    level=level-1;
    if (level<0) {level=4;}
   }
   if (Input.GetKeyDown(KeyCode.RightArrow))
   {
    level=level+1;
    if (level>4) {level=0;}
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
   for (i=0;i<=3;i++)
   {
    if (i=0)
  {
   obj[3].transform.Rotate(0,0,-0.1f);
   kumoxf=kumoxf+kumoxxf;
   if (kumoxf>=200f && kumoxxf>0f) {kumoxf=200f;kumoxxf=-0.1f;}
   if (kumoxf<=0f && kumoxxf<0f) {kumoxf=0f;kumoxxf=0.1f;}
   Vector3 pos1=obj[1].transform.position;
   pos1.x=kumoxf*0.01f-3f;obj[1].transform.position=pos1;
   Vector3 pos2=obj[2].transform.position;
   pos2.x=kumoxf*0.01f+1f;obj[2].transform.position=pos2;
  }
  if (flg==1)
  {
// inkey
   moves=0;
   if (Input.GetKeyDown(KeyCode.RightArrow)) {moves=1;}
   if (Input.GetKeyDown(KeyCode.LeftArrow)) {moves=2;}
   if (Input.GetKeyDown(KeyCode.UpArrow)) {moves=3;}
   if (Input.GetKeyDown(KeyCode.DownArrow)) {moves=4;}
   if (Input.GetKeyDown(KeyCode.D)) {moves=5;}
   if (Input.GetKeyDown(KeyCode.A)) {moves=6;}
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();flg=0;spacewait=2;}
// yoko (enko)
   if (moves==1)
   {
    p[px*9]=p[px*9+4+level];charaput(px,0,p[px*9]);
    flg=2;count=0;
   }
   if (moves==2)
   {
    a=p[px*9+1];
    for (i=0;i<=3+level;i++) {p[px*9+i]=p[px*9+i+1];}
    p[px*9+4+level]=a;
    for (i=0;i<=8;i++) {charaput(px,i,p[px*9+i]);}
    for (i=0;i<=8;i++)
    {
    a=p[px*9+i];
    obj[a*63+px*9+i+200].transform.Rotate(0,0,(-22.5f));
    }
    flg=3;count=0;
   }
// tate (to hole)
   if (moves==3 && anay<6)
   {
    a1=anay*9+anax;a2=a1+9;
    p[a1]=p[a2];p[a2]=0;
    charaput(anay,anax,p[a1]);charaput(anay+1,anax,0);
    anay=anay+1;hantei();
   }
   if (moves==4 && anay>0)
   {
    a1=anay*9+anax;a2=a1-9;
    p[a1]=p[a2];p[a2]=0;
    charaput(anay,anax,p[a1]);charaput(anay-1,anax,0);
    anay=anay-1;hantei();
   }
// ya-move
   if (moves==5 && px<6) {px=px+1;}
   if (moves==6 && px>0) {px=px-1;}
   if (moves==5 || moves==6) {z=4;zx=px*20-390;zy=-272;objput();}
   moves=0;
  }
  if (flg==2)
  {
   count=count+1;
   for (i=0;i<=8;i++)
   {
    a=p[px*9+i];
    obj[a*63+px*9+i+200].transform.Rotate(0,0,(-0.5f));
   }
   if (count>=45)
   {
    for (i=4+level;i>0;i--) {p[px*9+i]=p[px*9+i-1];}
    p[px*9]=0;
    for (i=0;i<=8;i++) {a=p[px*9+i];charaput(px,i,a);}
    kakudo_reset();
    if (anay==px)
    {
     anax=anax+1;
     if (anax>4+level) {anax=1;}
    }
    flg=1;hantei();
   }
  }
  if (flg==3)
  {
   count=count+1;
   for (i=0;i<=8;i++)
   {
    a=p[px*9+i];
    obj[a*63+px*9+i+200].transform.Rotate(0,0,(+0.5f));
   }
   if (count>=45)
   {
    p[px*9]=0;charaput(px,0,0);
    kakudo_reset();
    if (anay==px)
    {
     anax=anax-1;
     if (anax<1) {anax=4+level;}
    }
    flg=1;hantei();
   }
  }
  if (flg>=1 && flg<=3)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=72f) {bgmstop();bgm1.Play();stimef=Time.time;}
  }
  if (flg==99)
  {
   count=count+1;
   if (count%25==0 && count<275)
   {
    a=count/25;
    if (a%2==1) {z=11;zx=-2000;zy=-160;objput();z=12;zx=0;zy=-160;objput();}
    else {z=11;zx=0;zy=-160;objput();z=12;zx=-2000;zy=-160;objput();}
   }
   if (count>=400)
   {
    titleput();
    count=0;flg=0;
   }
  }
 }

 void gamestart()
 {
  alldel();
  for (i=level;i<=3;i++) {z=i+5;zx=0;zy=-260;objput();}
  for (i=0;i<=62;i++) {p[i]=0;}
  for (i=0;i<=6;i++)
  {
   for (j=1;j<=4+level;j++) {p[i*9+j]=i+1;}
  }
  for (i=0;i<1000;i++)
  {
   r1=Random.Range(0,7);r2=Random.Range(1,5+level);a1=r1*9+r2;
   r1=Random.Range(0,7);r2=Random.Range(1,5+level);a2=r1*9+r2;
   a=p[a1];p[a1]=p[a2];p[a2]=a;
  }
  r1=Random.Range(0,7);r2=Random.Range(1,5+level);a1=r1*9+r2;
  anay=r1;anax=r2;p[a1]=0;
  for (i=0;i<=6;i++)
  {
   for (j=0;j<=8;j++) {charaput(i,j,p[i*9+j]);}
  }
  stimef=Time.time;bgmstop();bgm1.Play();
  px=0;z=4;zx=px*20-390;zy=-272;objput();z=10;zx=0;zy=-160;objput();
  flg=1;
 }

 void hantei()
 {
  a1=1;a2=1;
  for (i=0;i<=6;i++)
  {
   for (j=1;j<=level+4;j++)
   {
    a=p[i*9+j];
    if (a!=0 && a!=i+1) {a1=0;}
    if (a!=0 && a!=7-i) {a2=0;}
   }
  }
  if (a1==1 || a2==1)
  {
   z=4;zx=-2000;zy=0;objput();z=10;objput();
   z=11;zx=0;zy=-160;objput();bgmstop();
   if (a1==1) {charaput(anay,anax,anay+1);bgm2.Play();}
   else
   {
    charaput(anay,anax,7-anay);bgm3.Play();
    obj[11].transform.Rotate(0,0,180);obj[12].transform.Rotate(0,0,180);
   }
   flg=99;
  }
 }

 void titleput()
 {
  alldel();
  for (i=0;i<=6;i++)
  {
   charaput(i,0,0);
   for (j=1;j<=8;j++) {charaput(i,j,i+1);}
  }
  z=9;zx=0;zy=-160;objput();
  for (i=level;i<=3;i++) {z=i+5;zx=0;zy=-260;objput();}
  bgmstop();spacewait=2;flg=0;
 }

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

 void charaput(int n1,int n2,int n3)
 {
  aaa=n1*9+n2+200;
  for (ii=0;ii<=7;ii++) {z=ii*63+aaa;zx=-2000;zy=0;objput();}
  if (n3>=0 && n3<=7)
  {
   z=aaa+n3*63;zx=0;zy=-260;objput();
  }
 }

 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();
 }

 void kakudo_reset()
 {
  for (i=0;i<=7;i++)
  {
   for (j=0;j<=6;j++)
   {
    for (k=0;k<=8;k++)
    {
     obj[i*63+j*9+k+200].transform.rotation=Quaternion.identity;
     obj[i*63+j*9+k+200].transform.Rotate(0,0,-22.5f*k+101.25f);
    }
   }
  }
 }

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  for (i=4;i<=12;i++) {z=i;zx=-2000;zy=0;objput();}
  for (i=11;i<=12;i++) {obj[i].transform.rotation=Quaternion.identity;}
  for (i=100;i<=155;i++) {z=i;zx=-2000;zy=0;objput();}
 }
}