package j;

import java.util.Calendar;
import java.util.Date;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

class DataStorage {
    
    public static RecordStore recordStore;
    public static String EmptyValue0="0"; 
    public static String EmptyValue1="00";
     public static String EmptyValue2="0"; 
    public DataStorage(String storeName) {
        openRecordStore(storeName);
    }
    
    private void openRecordStore(String storeName) {
        try {
            recordStore = RecordStore.openRecordStore(storeName, true);
        if(recordStore.getSize()>0){
            System.out.println();
            
        }
        
        
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public static void closeRecordStore() {
        try {
            if (recordStore != null) {
                recordStore.closeRecordStore();
            }
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public static void addData(String data) {
        try {
          
            byte[] byteData = data.getBytes();
            recordStore.addRecord(byteData, 0, byteData.length);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }}
    
 
    public static void getAllData() {
        try {
            System.out.println("Readen Successfuly");
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            int numRecords = enumeration.numRecords();
            String[] data = new String[numRecords];
            int index = 0;
            int i=0;
            while (enumeration.hasNextElement()) {
                i++;
                
                int recordId = enumeration.nextRecordId();
                byte[] record = recordStore.getRecord(recordId);
                data[index++] = new String(record);
              
                j.Midlet0.TTT[i-1]=new String(record);
            // System.out.println((i-1)+"---"+j.Midlet0.TTT[i-1]);
             
            }
            enumeration.destroy();
            //return data;
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
       /// return null;
    }
 
    public static void deleteAllData() {
        try {
            System.out.println("delted");
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            while (enumeration.hasNextElement()) {
                int recordId = enumeration.nextRecordId();
                recordStore.deleteRecord(recordId);
            }
            enumeration.destroy();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }

public static void setRecord(int i, String str)
{
 try
  {byte[] rec = str.getBytes();
      j.DataStorage.recordStore.setRecord(i, rec, 0, rec.length);
    }
    catch (Exception e ){
 
    }
  }
}
class Midlet0 extends Canvas {
    public static boolean F0 = true;
    public static Command Command2=null;
    public static Command Command3=null;
    public static int I=0; //index of current item TTT 
    public static int II=1;// index of current item recordstore
    public static short F0000=0; 
    public static int XX=0;
    public static int YY=0;

   
    public static int N0=9;       //Test Period During. Not currently in use.
    public static int N1=0;      //Progress usless item.
    public static int N2=0;     //Progress ddd.
    public static int N3=0;    //Progress usless item.
    public static int N4=333; //Begin progress ppp.
    public static int N5=0;   //N2-N4 [progress from Begin] 
    public static int N6=0;   //DDD-N2; [left ddd]
    public static int N7=0;    //[Empty/Filled event sectors] 
    public static int N8=367;   //TTT.length from DDDM
    public static String TTT[];
    public static short xSize=14;
    public static short ySize=7;
    public static short xDelta=1;
    public static short yDelta=1;
    public static int ii=0;
    public static Canvas canvas0=null;
    public static Canvas canvas1=null;
    public static Canvas canvas2=null;
    
    //public static int N0000=10;
    //public static Image image=null;
  //E0000
    
    public static void LoadSettings(){
      
      try {
          
         
          
          
//image = Image.createImage("res/0.png");
        j.Screen0.UpdateTime();
       // j.Screen0.UpdateTime();
        j.Screen0.getDDD();
        //System.out.println("D0002: " +  j.Screen0.D0002);
        N2=Screen0.getDDD();
        N5=N2-N4; 
        N6=j.Screen0.DDD-N2;
        N8=j.Screen0.DDD;
        
        
        
    } catch (Exception e) {
        e.printStackTrace();
    }
  }   
 public static void Storage_To_Array_0() throws RecordStoreNotOpenException, RecordStoreException{
        
       // j.RW0.openRecordsStore_0();
        //for(int i=1;i<=rc.getNumRecords();i++){
        //ReadHot();
       
       j.DataStorage.getAllData();     
//}    
        
      //  j.RW0.closeRecStore_0();
        }
     

 public static void  Array_To_Storage_0() throws RecordStoreNotOpenException{
        try{
        System.out.println("TTT to ROM has been begun");     
//j.RW0.openRecordsStore_0();
        for(int i=1;i<=j.DataStorage.recordStore.getNumRecords();i++){
            if(TTT[i-1].length()>0){
            j.DataStorage.setRecord(i,j.Midlet0.TTT[i-1]); }
            else{
            j.DataStorage.setRecord(i, j.DataStorage.EmptyValue0);
//System.out.println("TTT to ROM was Complited");
        }}}
 
        catch(Exception ex){ex.printStackTrace();} 
        System.out.println("TTT to ROM was Complited");        
 }
 
 
 public static void Create_Storage_By_Append(int Length0) throws RecordStoreNotOpenException{
  if(!(j.DataStorage.recordStore.getNumRecords()>0)){
  for(int i=1;i<=Length0;i++){
        j.DataStorage.addData(j.DataStorage.EmptyValue0);
         
  }}
  
 System.out.println("Created ROM And Loaded to TTT");
 }
 
 
 
     public void paint(Graphics g) {
    
    g.setColor(0,0,0);
    g.fillRect(0, 0, 400, 400);     
    
    //g.setColor(100,100,46);  
    g.setColor(240,230,63);// Color Yellow!!
    for(int y=0;y<12;y++){   
        for(int x=0;x<j.Screen0.m[y];x++){
          
            g.fillRect((yDelta+xSize)*y,(xDelta+ySize)*x , xSize, ySize);
    
    }}
    
    g.setColor(22,22,255);//Color Blue for empty/full Array/RecordStore 
    N7=0;
    for(int y=0;y<12;y++){ 
        for(int x=0;x<j.Screen0.m[y];x++){
            if(N7<=N8-1){
   if((TTT[N7].length()!=0) && !(TTT[N7].equals(j.DataStorage.EmptyValue0)) && 
   !(TTT[N7].equals(j.DataStorage.EmptyValue1))){ 
              g.fillRect((yDelta+xSize)*y,(xDelta+ySize)*x , xSize, ySize);}
                N7++;
            
            
            }
                
          }}
    
    N1=0;
    g.setColor(102,160,196); //Light Blue!!
   int DM = j.Screen0.D0000;   // добор текущего дня месяца
   int MM = j.Screen0.D0001+1; // добор текущего месяца с корректировкой
  // N2=28+31+1;
  //System.out.println("D000: "+ j.Screen0.D0000);
    for(int y=0;y<12;y++){ 
        for(int x=0;x<j.Screen0.m[y];x++){
            if(N1<N2){
            N1++;
            g.fillRect((yDelta+xSize)*y,(xDelta+ySize)*x , xSize, ySize); }}
            
    }
    //j.RW0.printing(N2+" ! ");
    N3=0; 
    g.setColor(0,255,100);//Color GREEN
    
    for(int y=0;y<12;y++){ 
        for(int x=0;x<j.Screen0.m[y];x++){
            if(N3<N5){
            N3++;
            g.fillRect((yDelta+xSize)*y,(xDelta+ySize)*x , xSize, ySize); 
            
    
    }}}
   // j.RW0.printing(N5+" ! ");
  
    g.setColor(255,0,0);//Color RED for ddd today Sector 
    g.fillRect((yDelta+xSize)*(Screen0.D0001),(xDelta+ySize)*(Screen0.D0000-1), xSize, ySize); 
    
    g.setColor(255,255,255);//Color WHITE for Curent Selection Sector  
    g.fillRect((yDelta+xSize)*XX,(xDelta+ySize)*YY , xSize, ySize); 
    
 
  
    g.setStrokeStyle(Graphics.SOLID);
    g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
    //g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
    g.setColor(240,230,63);//yellow 
    //g.drawString(N2+"/"+N6+"/"+N5, 20,265,g.TOP|g.LEFT);
    //if(N5>=10 && N5<20){g.drawString("Mission Complete!!!", 50,265,g.TOP|g.LEFT);}    
    //else if(N5>=20){g.drawString("Level 2 opened! Java!", 50,265,g.TOP|g.LEFT);}    
   // g.drawString("I: "+I+", "+"II: "+II+", " +TTT[I], 10,265,g.TOP|g.LEFT);
    
    g.drawString((II)+":  "+Screen0.D0002+"/"+(XX+1)+"/"+(YY+1)+ ":   " +TTT[I], 10,265,g.TOP|g.LEFT);    
//try {  Thread.sleep(1000);} catch (Exception ex) { ex.printStackTrace();}  
    //repaint();
  }

  public static void Stepper() {
  
  if(II>0 && II<j.Screen0.DDD){
  
  }
  

  I= date_To_DDD(j.Screen0.DDD,XX,YY+1);
  
   
   if (I - 1 >= 0) {  I--; II=I+1;     }   
   else if(I - 1 < 0){I = j.Screen0.DDD - 1;  II=I+1; }
   else if(I + 1 <= j.Screen0.DDD-1){I++; II=I+1;    }
   else if(I + 1 > j.Screen0.DDD-1){I = 1; II=I+1;  }
 
 //j.RW0.printing((I+"_"+ II),     j.Midlet0.F0);
  
   //Midlet.Load_Item_From_Array(4);
  }   
     
     
    
   public static int date_To_DDD(int YYYY,int MM, int MD){ 
    // if(MM<12 && MM>=0 && MD>=1 && MD<=31){
     
     int max_DDD=0;
     int Fe=28;
     int ddd=0;
     int I=0;
     if (YYYY%4!=0){ Fe=28;}
     else{Fe=29;}
     if(Fe==29){max_DDD=366;}
     int[]m= new int[12];
     
     m[0]=31;
     m[1]=Fe;
     m[2]=31;
     m[3]=30;
     m[4]=31;
     m[5]=30;
     m[6]=31;
     m[7]=31;
     m[8]=30;
     m[9]=31;
     m[10]=30;
     m[11]=31; 
     
  if(MM<12 && MM>=0 && MD>=1 && MD<=31){
  
     
     for(int i =0;i<=MM-1;i++){ddd=ddd+j.Screen0.m[i];}
     I=ddd+MD;}
  
  
  
  else{
      System.out.println("Month or Month Day is over Bounds");}
  
   //  j.RW0.printing("DDD_out:" + I);
    // System.out.println("YYYY: "+ YYYY+ " MM: "+   MM+  " MD: " + MD +"  DDD: "+I);
     //}
       //System.out.println("DDD: "+ DDD+ "+D0003: " + D0003+"_ D0000: " + D0000);
        return I; 
        
        
        
        
        
        
        
        
    }
    
  

  public void keyPressed(int keyCode) {
      /*  
       switch((getGameAction(keyCode))){      
        
            //j.RW0.printing("Fire is pressed");
         
          
     
       case Canvas.UP:
            YY--;
            break;
           
       case Canvas.DOWN:
            YY++;
            break;         
       case Canvas.RIGHT:
            
            XX++;
            break;
       case Canvas.LEFT:
            XX--;
            break;
  
            }
           */
 switch(keyCode){
       case -1:
            YY--;
            break;    
       
       case -2:
            YY++;
            break;  
           
       case -3: 
            XX--;
            break;
           
       case -4:
            XX++;
            break;
           
       case 50:
            YY=YY-10;
            break; 
           
       case 56:
            YY=YY+10;
            break; 
           
       case 54: 
            XX=XX+3;
            break;
           
       case 52:
            XX=XX-3; 
            break; 
           
       case 53:
            XX=Screen0.D0001;  YY=Screen0.D0000-1;
            break;
           
       case -5:
            j.Screen0.display.setCurrent(Form0.Form0);
            break;     
          
          }
 /*
          if(keyCode==50){YY=YY-10;}
          else if(keyCode==56){YY=YY+10;}  
          else if(keyCode==54){XX=XX+3;}
          else if(keyCode==52){XX=XX-3;}
           else if(keyCode==53){XX=Screen0.D0001;YY=Screen0.D0000-1;
           //Screen0.Load_Item_From_Array(4);
           }
           else if(keyCode==-5){j.Screen0.display.setCurrent(Form0.Form0);}
          */
          
       // j.RW0.printing("_ int keCode :"+keyCode, true);        
       
    
            
  
  Stepper();
  repaint();           
 
/*
   else if(getKeyName(keyCode).equals("Clear")){
   if (TTT[I].length() > 0) {
    TTT[I] = TTT[I].substring(0, (TTT[I].length() - 1));
*/
   
      
         

   
  }  
   
          
   
    
/*
public void keyReleased(int keyCode) { 
switch((getGameAction(keyCode))){      
       
       case Canvas.UP:
            YY--;
            break;
       case Canvas.DOWN:
            YY++;
            break;       
       case Canvas.RIGHT:
            XX++;
            break;
       case Canvas.LEFT:
            XX--;
            break; 
        case Canvas.KEY_NUM0:
            //F0000=2;
        
            break; 
            //Generator         
    } 

  I= date_To_DDD(j.Screen0.D0002,XX,YY+1);
            
            Steper();
            System.out.println(YY+"__ " + XX +"__"+ I);
   repaint ();

}


 */

}

public class Screen0 extends MIDlet {
    
    public static Display display;
    public static RecordStore RS; 
    public static String RecordStotreName;
    public static Calendar c0;
    public static Date moment;
    public static String ReadTime;
    public static int m[];
    public static String DDDD;
    public static String T0000;
    public static int D0000;//DM
    public static String T0001;
    public static int D0001;//MM
    public static String T0002;
    public static int D0002;//YYYY
    public static int Fe=28;
    public static int DDD=366;//Max days in Year
    public static int D0003;//ddd
    public static String TimeString;
    public static void UpdateTime(){
    Calendar c0= Calendar.getInstance();
    Date moment= c0.getTime();
    ReadTime= String.valueOf(moment);
    DDDD= String.valueOf(c0.get(Calendar.DATE));//Date (in String Format)
    T0000= String.valueOf(c0.get(Calendar.DAY_OF_MONTH));
    D0000= Integer.parseInt(T0000);//DM
    T0001= String.valueOf(c0.get(Calendar.MONTH));
    D0001= Integer.parseInt(T0001);//MM
    T0002= String.valueOf(c0.get(Calendar.YEAR));
    D0002= Integer.parseInt(T0002);//YYYY
    D0003=0;//ddd bayc full;
    
    }
    public static int getDDD(){
    D0003=0;
     Fe=28;
     int ddd=0;
     if (D0002%4!=0){ Fe=28;}
     else{Fe=29;}
     if(Fe==29){DDD=366;}
     m= new int[12];
     //m[0]=0;
     m[0]=31;
     m[1]=Fe;
     m[2]=31;
     m[3]=30;
     m[4]=31;
     m[5]=30;
     m[6]=31;
     m[7]=31;
     m[8]=30;
     m[9]=31;
     m[10]=30;
     m[11]=31;
     for(int i =0;i<D0001;i++){ddd=ddd+m[i];}
        D0003=ddd+D0000;
       System.out.println("YYYY: "+ D0002 +"  DDD: "+ DDD+ " D0003: " + D0003+" D0000: " + D0000);
        return D0003; 
        
        
        
        
        
        
        
        
    }
    
    
    
    //public static int AddToUpdateDDD_Current_DM(int Corrected_Month){
      //  return m[]
    //}
    
   public static void Create_Array_0(int L){
        j.Midlet0.TTT= new String[L];
        for(int i=0;i<=L-1;i++){
        j.Midlet0.TTT[i]=j.DataStorage.EmptyValue1;
        
       
       //  System.out.println("TTT[ "+ i+ "] is:"+ j.Midlet0.TTT[i] );
        
        }
        
        //j.Midlet0.TTT[364]="acer"; 
        System.out.println("TTT.length: "+ j.Midlet0.TTT.length +" was created");
   }
   
    
   
  public static void Load_Item_From_Array(int i){
  
  String str=j.Midlet0.TTT[i-1];
  
  
  if(str.length()>0 && !str.equals(j.DataStorage.EmptyValue2)){
  Midlet0.N4 = Integer.parseInt(str);
          }
  Midlet0.N5=Midlet0.N2-Midlet0.N4;
  //Midlet0.TTT[Midlet0.I]=; 
            
  }
  
 protected void startApp() {
 try {     
          
          j.Midlet0.canvas0 = new Midlet0();
          j.Midlet0.LoadSettings(); 
          DataStorage dataStorage = new DataStorage("ZZZZ");
          j.Screen0.Create_Array_0(366);//Set Maximum Array Length/
          j.Midlet0.Create_Storage_By_Append(366);
        
          j.Midlet0.Storage_To_Array_0();
        //  j.RW0.closeRecStore_0();
          j.Midlet0.N8=366;
          Load_Item_From_Array(4);
          DDD=366; 
          UpdateTime();
          
         // j.Screen0.D0002=2024; //Activate For testing 366-ed year.
          j.Midlet0.N2=getDDD();
          


          
             
          
          
          
          
           //java.lang.System.gc();
           //j.Screen0.DDD=365;
System.out.println( "Successfuly startMidlet:  "+ "N2: " +j.Midlet0.N2+ " N6: "+ j.Midlet0.N6 +" N5: "+ j.Midlet0.N5 + " DDD: " + j.Screen0.DDD +" D0002: " + j.Screen0.D0002);
//System.out.println("!");
 }         catch (Exception ex) {   ex.printStackTrace();}
    
j.Midlet0.Command2 = new Command("Left",Command.EXIT,0);
j.Midlet0.Command3 = new Command("Right",Command.OK,1);

//Command1 = new Command("clear",Command.SCREEN,1);
j.Midlet0.canvas0.addCommand(j.Midlet0.Command2);
j.Midlet0.canvas0.addCommand(j.Midlet0.Command3);
//Form0.addCommand(Command1);
//Form0.setCommandListener(this);

j.Midlet0.canvas0.setCommandListener(new CommandListener() {
     

    public void commandAction(Command c, Displayable d) {
  
      // j.RW0.printing("Comma:: ");
        if(c==Midlet0.Command2) {
                                //j.RW0.printing("Left:: ", Midlet0.F0);
                                j.Screen0.display.setCurrent(j.Midlet0.canvas2);  
                                }
        
        if(c==Midlet0.Command3){
                             //  j.RW0.printing("Reght:: ", Midlet0.F0);
                               j.Screen0.display.setCurrent(j.Midlet0.canvas2);  
                               }
                                                         }
                             });

 
 
 
    
    
    display = Display.getDisplay(this);
    display.setCurrent(j.Midlet0.canvas0);
   
    
    j.Form0.Start0();
    j.Midlet0.canvas2=new Timer0();
    j.Timer0.Start3();
    
    
    
    
    
    
 
 
 
 
 }
 
  protected void pauseApp() {
  try {
   }    
     
    catch (Exception ex) { ex.printStackTrace();         
  }
  }
  
  protected void destroyApp(boolean unconditional) {
   try {
          
        // j.DataStorage.deleteAllData();
        // j.RW0.openRecordsStore_0();
        // Midlet0.TTT[Screen0.D0003-1]="";
         Midlet0.TTT[Screen0.D0003-1]=j.Timer0.countervector + " sec";
         j.Midlet0.Array_To_Storage_0();  
         System.out.println("Almoust Completed");
         
         j.DataStorage.closeRecordStore();
         //try{Thread.sleep(4000);}
         //catch(Exception ex){ex.printStackTrace();}
        notifyDestroyed();     
   }      
    catch (Exception ex) { ex.printStackTrace();}

}
}

