#include "func.h"


#define intervalin for(int i = 0;i<intervalincell;i++)putchar(symb3);
#define intervaln for(int i = 0;i<intervaly;i++)putchar('\n');
#define intervalbt for(int i = 0;i<intervalx;i++)putchar(symb3);
#define dot *(*(massive+col)+row) = '@';

void mseehelp(int i){
        putchar(symb1);
        intervalin;
        printf("%lc",i);
        intervalin;
        putchar(symb2);
        intervalbt;
}
void cseehelp(int i){
        putchar('[');
        intervalin;
        putwchar(i);
        intervalin;
        putchar(']');
        intervalbt;
}

void seeMassive(int (*massive)[COL]){
    printf("\t\t\tMassive\n");

//corner null
cseehelp(symb3);

// top 0-9
    for(char i = '0';i<=(COL+'0');i++){
    if(i!=COL+'0')cseehelp(i);
    else cseehelp(symb3);
    }
    intervaln;

// Massive
    for(char i = '0';i<=(ROW+'0');i++){
        if(i!=ROW+'0'){
            cseehelp(i);//left 0-9
            for(char j = '0';j<(COL+'0');j++){
                    mseehelp(*(*(massive+(i-'0'))+(j-'0')));//symbols
    
            }
            cseehelp(i);//right 0-9
            intervaln;
        }
        else{ // bottom 0-9
            cseehelp(symb3);
            for(char i = '0';i<=(COL+'0');i++){
                if(i!=COL+'0')cseehelp(i);
                else cseehelp(symb3);
            }   
            intervaln;
       }
    }
    return;
}

void setMassive(int settype, int (*massive)[COL]){
    int re, inbuff=0, buffcmd=1, cell=-1;
    int x=0,y=0,st=0;
    int row=0,col=0;
    int buff[5];
    switch(settype){
        case 21:
            while(buffcmd!=-1){
                if(row==-1)row=9;
                re = *(*(massive+col)+row);
                dot;
                clear();
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            if(row<ROW&&col<COL){
                                *(*(massive+col)+row)=*(buff+i);
                                row++;}
                            else if(col<COL-1){
                                row=0;col++;
                                *(*(massive+col)+row)=*(buff+i);
                                row++;}
                            else buffcmd=-1;
                        }
                        break;
                    case 1:
                        *(*(massive+col)+row) = re;
                        row = col = 0;
                        for(;cell>0;){
                        if((cell-10)>=0){cell-=10;col++;}
                        else {cell--;row++;}
                        }
                }
            }
            *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;

         case 22:
            while(buffcmd!=-1){
                re = *(*(massive+col)+(row+9));
                dot;
                clear();
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            if(row>(ROW*-1)&&col<COL){
                                *(*(massive+col)+(row+9))=*(buff+i);
                                row--;
                                if(row==-10){row=0;col++;}
                                }
                            else buffcmd=-1;
                        }
                        break;
                    case 1:
                        *(*(massive+col)+(row+9)) = re;
                        row =0; col = 0;
                        for(;cell>0;cell--){
                        if((cell-10)>=0){cell-=10;col++;}
                        else {cell--;row--;}
                        }
                }
            }
            *(*(massive+col)+(row+9)) = re;
            helpMenu();printf("Input: ");
            break;

         case 23:
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                clear();
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            if(row<ROW&&col<COL){
                                *(*(massive+col)+row)=*(buff+i);
                                col++;    
                                if(col==10){col=0;row++;}
                                }
                            else buffcmd=-1;
                        }
                        break;
                    case 1:
                        *(*(massive+col)+row) = re;
                        row = 0; col = 0;
                        for(;cell>0;){
                        if((cell-10)>=0){cell-=10;row++;}
                        else {cell--;col++;}
                        }
                }
            }
            *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;

         case 24:
            while(buffcmd!=-1){
                re = *(*(massive+(col+9))+row);
                dot;
                *(*(massive+(col+9))+row) = 'X';
                clear();
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            if(row<ROW&&col>(COL*-1)){
                                *(*(massive+(col+9))+row)=*(buff+i);
                                col--;
                                printf("\t%d %d\n",col,col+9);
                                if(col==-10){col=0;row++;}
                                }
                            else buffcmd=-1;
                        }
                        break;
                    case 1:
                        *(*(massive+(col+9))+row) = re;
                        row = 0; col = 0;
                        for(;cell>0;){
                        if((cell-10)>=0){cell-=10;row++;}
                        else {cell--;col--;}
                        }
                }
            }
            *(*(massive+(col+9))+row) = re;
            helpMenu();printf("Input: ");
            break;

         case 25:
            x=1,y=1,st=1;
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                clear();
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            switch(st){
                                case 1:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row++;
                                    if(row==COL-y){st=2;}
                                    break;
                                case 2:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col++;
                                    if(col==row)st=3;
                                    break;
                                case 3:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row--;
                                    if(row==x-1){st=4;y++;}
                                    break;
                                case 4:
                                    *(*(massive+col)+row)=*(buff+i);
                                    if(row==4&&col==5)st=0;
                                    col--;
                                    if(col==row+1){st=1;x++;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                                    break;
                            }}
                        break;

                    case 1:
                        *(*(massive+col)+row) = re;
                        col=row=0;x=y=1;st=1;
                        for(;cell>0;cell--){
                            switch(st){
                                case 1:
                                    row++;
                                    if(row==COL-y){st=2;}
                                    break;
                                case 2:
                                    col++;
                                    if(col==row)st=3;
                                    break;
                                case 3:
                                    row--;
                                    if(row==x-1){st=4;y++;}
                                    break;
                                case 4:
                                    if(row==4&&col==5)st=0;
                                    col--;
                                    if(col==row+1){st=1;x++;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                            }
                        }
                   }
                }
            *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;


         case 26:
            x=5,y=5,st=1,row=4,col=5;
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            switch(st){
                                case 1:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row++;
                                    if(row==col){st=2;}
                                    break;
                                case 2:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col--;
                                    if(col==x-1)st=3;
                                    break;
                                case 3:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row--;
                                    if(col==row+1){st=4;y--;}
                                    if(row==-1)st=0;
                                    break;
                                case 4:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col++;
                                    if(col==COL-y){st=1;x--;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                                    break;
                            }}
                        break;

                    case 1:
                        *(*(massive+col)+row) = re;
                        x=5,y=5,st=1,row=4,col=5;
                        for(;cell>0;cell--){
                            switch(st){
                                case 1:
                                    row++;
                                    if(row==col){st=2;}
                                    break;
                                case 2:
                                    col--;
                                    if(col==x-1)st=3;
                                    break;
                                case 3:
                                    row--;
                                    if(col==row+1){st=4;y--;}
                                    if(row==-1)st=0;
                                    break;
                                case 4:
                                    col++;
                                    if(col==COL-y){st=1;x--;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                            }}

                   }
                }
            *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;


         case 27:
            x=1,y=9,st=3,row=0,col=9;
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            switch(st){
                                case 1:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row++;
                                    if(row==x){st=2;x++;}
                                    break;
                                case 2:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col++;
                                    if(col==(COL-1)){st=3;}
                                    break;
                                case 3:
                                    *(*(massive+col)+row)=*(buff+i);
                                    if(x==ROW){st=0;buffcmd=-1;}
                                    else{y--;col = y;row=0;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}
                        break;
                    case 1:
                        *(*(massive+col)+row) = re;
                        x=1,y=9,st=3,row=0,col=9;
                        for(;cell>0;cell--){
                            switch(st){
                                case 1:
                                    row++;
                                    if(row==x){st=2;x++;}
                                    break;
                                case 2:
                                    col++;
                                    if(col==(COL-1)){st=3;}
                                    break;
                                case 3:
                                    if(x==ROW){st=0;buffcmd=-1;}
                                    else{y--;col = y;row=0;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}

                }
            }
            helpMenu();printf("Input: ");
            break;


        case 28:
            x=8,y=0,st=3,row=9,col=0;
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            switch(st){
                                case 1:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row--;
                                    if(row==x){st=2;x--;}
                                    break;
                                case 2:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col--;
                                    if(col==0){st=3;}
                                    break;
                                case 3:
                                    *(*(massive+col)+row)=*(buff+i);
                                    if(x==-1){st=0;buffcmd=-1;}
                                    else{y++;col = y;row=9;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}
                        break;
                    case 1:
                        *(*(massive+col)+row) = re;
                        x=8,y=0,st=3,row=9,col=0;
                        for(;cell>0;cell--){
                            switch(st){
                                case 1:
                                    row--;
                                    if(row==x){st=2;x--;}
                                    break;
                                case 2:
                                    col--;
                                    if(col==0){st=3;}
                                    break;
                                case 3:
                                    if(x==-1){st=0;buffcmd=-1;}
                                    else{y++;col = y;row=9;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}
                }
            }
            *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;



        case 29:
            x=1,y=0,st=3,row=0,col=0;
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            switch(st){
                                case 1:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row++;
                                    if(row==x){st=2;x++;}
                                    break;
                                case 2:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col--;
                                    if(col==0){st=3;}
                                    break;
                                case 3:
                                    *(*(massive+col)+row)=*(buff+i);
                                    if(col==0&&row==ROW-1){st=0;buffcmd=-1;}
                                    else{y++;col = y;row=0;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}
                        break;
                    case 1:
                        x=1,y=0,st=3,row=0,col=0;
                        *(*(massive+col)+row) = re;
                        for(;cell>0;cell--){
                            switch(st){
                                case 1:
                                    row++;
                                    if(row==x){st=2;x++;}
                                    break;
                                case 2:
                                    col--;
                                    if(col==0){st=3;}
                                    break;
                                case 3:
                                    if(col==0&&row==ROW-1){st=0;buffcmd=-1;}
                                    else{y++;col = y;row=0;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}

                }
            }
                        *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;

         case 210:
            x=8,y=9,st=3,row=9,col=9;
            while(buffcmd!=-1){
                re = *(*(massive+col)+row);
                dot;
                printf("\t\t\tMASSIVE EDIT\nInput:\n\t\"-num-\" to select cell\n\t \"exit\" to leave\n");    
                //printf("\ncol:%d  row:%d  x:%d  y:%d st:%d",col,row,x,y,st);
                seeMassive(massive);
                printf("Massive input: ");
                buffcmd = buffinput(&cell,&inbuff,buff);
                switch(buffcmd){
                    case -1:break;
                    case 0:
                        for(int i = 0;i<inbuff;i++){
                            switch(st){
                                case 1:
                                    *(*(massive+col)+row)=*(buff+i);
                                    row--;
                                    if(row==x){st=2;x--;}
                                    break;
                                case 2:
                                    *(*(massive+col)+row)=*(buff+i);
                                    col++;
                                    if(col==(COL-1)){st=3;}
                                    break;
                                case 3:
                                    *(*(massive+col)+row)=*(buff+i);
                                    if(col==COL-1&&row==0){st=0;buffcmd=-1;}
                                    else{y--;col = y;row=9;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}
                        break;
                    case 1:
                        *(*(massive+col)+row) = re;
                        x=8,y=9,st=3,row=9,col=9;
                        for(;cell>0;cell--){
                            switch(st){
                                case 1:
                                    row--;
                                    if(row==x){st=2;x--;}
                                    break;
                                case 2:
                                    col++;
                                    if(col==(COL-1)){st=3;}
                                    break;
                                case 3:
                                    if(col==COL-1&&row==0){st=0;buffcmd=-1;}
                                    else{y--;col = y;row=9;st=1;}
                                    break;
                                case 0:
                                    buffcmd=-1;
                        }}
                }
            }
                        *(*(massive+col)+row) = re;
            helpMenu();printf("Input: ");
            break;

    }
}
