package jforex;
 
import com.dukascopy.api.*;
import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.pretty_tools.dde.client.DDEClientConversation;
import com.pretty_tools.dde.client.DDEClientException;
import java.util.Arrays;
import java.util.Currency;
import java.util.HashSet;
import java.util.Set;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IMessage.Type;
import java.util.Iterator;

 
@Library("pretty-tools-JDDE-1.0.2.jar")
 
 
 
@RequiresFullAccess
public class MoneyMangement implements IStrategy {
     
       
   
    
    @Configurable(value="Separator",description="Decimal separator in Excel")
    public String decSepa = ",";
    
    @Configurable(value="Sheet name",description="Name of the sheet in Excel. By default, leave it is as it")
    public String  sheetName = "DDEJFOREX";
    
    @Configurable(value="Language of Excel",description="EN = english, FR=french, GE=german")
    public String lang="FR";
    
    public Instrument[] instrumentArr= new Instrument[21];
    
   
    
     
    public String myCurrency="";
    private IContext context;
    private DDEClientConversation conversation; 
    private IConsole console;
    private final Set<Instrument> instruments = new HashSet<Instrument>(Arrays.asList(new Instrument[] {
        Instrument.AUDCAD,
        Instrument.AUDCHF,
        Instrument.AUDJPY,
        Instrument.AUDUSD,
        Instrument.CADCHF,
        Instrument.CADJPY,
        Instrument.CHFJPY,
        Instrument.EURAUD,
        Instrument.EURCAD,
        Instrument.EURCHF,
        Instrument.EURGBP,
        Instrument.EURJPY,
        Instrument.EURUSD,
        Instrument.GBPAUD,
        Instrument.GBPCAD,
        Instrument.GBPCHF,
        Instrument.GBPJPY,
        Instrument.GBPUSD,
        Instrument.USDCAD,
        Instrument.USDCHF,
        Instrument.USDJPY
       
          
    }));
    
    private JFUtils utils;
    private IEngine engine;
    
 
     
    @Override
    public void onStart(IContext context) throws JFException {
         
      
        console = context.getConsole();
        context.setSubscribedInstruments(instruments);
        engine=context.getEngine();
        int i = 21;
        while (!context.getSubscribedInstruments().containsAll(instruments)) {
            try {
                console.getOut().println("Instruments not subscribed yet " + i);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                console.getOut().println(e.getMessage());
            }
            i--;
        }
        
             
        instrumentArr[0]=Instrument.AUDCAD;
        instrumentArr[1]=Instrument.AUDCHF;
        instrumentArr[2]=Instrument.AUDJPY;
        instrumentArr[3]=Instrument.AUDUSD;
        instrumentArr[4]=Instrument.CADCHF;
        instrumentArr[5]=Instrument.CADJPY;
        instrumentArr[6]=Instrument.CHFJPY;
        instrumentArr[7]=Instrument.EURAUD;
        instrumentArr[8]=Instrument.EURCAD;
        instrumentArr[9]=Instrument.EURCHF;
        instrumentArr[10]=Instrument.EURGBP;
        instrumentArr[11]=Instrument.EURJPY;
        instrumentArr[12]=Instrument.EURUSD;
        instrumentArr[13]=Instrument.GBPAUD;
        instrumentArr[14]=Instrument.GBPCAD;
        instrumentArr[15]=Instrument.GBPCHF;
        instrumentArr[16]=Instrument.GBPJPY;
        instrumentArr[17]=Instrument.GBPUSD;
        instrumentArr[18]=Instrument.USDCAD;
        instrumentArr[19]=Instrument.USDCHF;
        instrumentArr[20]=Instrument.USDJPY;
        
        
        
        
        
         conversation = new DDEClientConversation(); 
        
         
        try {
            conversation.connect("Excel", sheetName);
             utils=context.getUtils();
            } catch (DDEClientException e) {
            print(e.toString());
            }
      
         
    }
     
    public synchronized void onBar(Instrument instrument, Period period, IBar askBar,
            IBar bidBar) throws JFException {
                
            }
         
  
     
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       try {
             Currency currency = Currency.getInstance(myCurrency);
             Iterator it = instruments.iterator();
             int rows=3;
             String[] strInsBid=new String[21];
             String[] strInsAsk=new String[21];
             double curr1Ask;
             double curr1Bid;
              
             for (int i =0; i<instrumentArr.length;i++)
             {
             if (instrument!=null && instrumentArr[i]==instrument)
                 {
                  strInsAsk[i]= String.valueOf(tick.getAsk());
                  strInsBid[i]= String.valueOf(tick.getBid());
                  conversation.poke(getCell(rows+i,1),instrumentArr[i].toString());
                  conversation.poke(getCell(rows+i,2),convertToDecimal( strInsAsk[i]));
                  conversation.poke(getCell(rows+i,3),convertToDecimal(strInsBid[i]));   
                  curr1Ask=utils.convertPipToCurrency(instrumentArr[i],currency,OfferSide.ASK);
                  Double Curr1Ask = new Double (curr1Ask);
                  conversation.poke(getCell(rows+i,5),convertToDecimal(Curr1Ask.toString()));   
                  curr1Bid=utils.convertPipToCurrency(instrumentArr[i],currency,OfferSide.BID);
                  Double Curr1Bid = new Double (curr1Bid);
                  conversation.poke(getCell(rows+i,6),convertToDecimal(Curr1Bid.toString()));   
                  conversation.poke(getCell(rows+i,9),String.valueOf(instrumentArr[i].getPipScale()));
                 
               }
             
             }    
                
        } catch (DDEClientException e) {
                 
                print(e.toString());
            }        
        
        
    }
 
    @Override
    public void onAccount(IAccount account) throws JFException {
         try {
           
            
            myCurrency = account.getCurrency().toString(); //Get currency of the account
            conversation.poke(getCell(2,12),myCurrency);
           
            double equity=account.getBaseEquity(); //Get the base equity
            Double MyEquity=new Double(equity);
            conversation.poke(getCell(3,12),convertToDecimal(MyEquity.toString()));
            
            double balance=account.getBalance(); //Get the balance
            Double MyBalance= new Double(balance);
            conversation.poke(getCell(4,12),convertToDecimal(MyBalance.toString()));
            
            double leverage = account.getLeverage();
            Double MyLeverage=new Double(leverage);
             conversation.poke(getCell(5,12),convertToDecimal(MyLeverage.toString()));
            
                       
 
        } catch (DDEClientException e) {
            print(e.toString());
        }
 
    }
 
    @Override
    public void onMessage(IMessage message) throws JFException {
    
  }
 
    @Override
    public void onStop() throws JFException {
 
    }
     
    public void print(String str) {
         
        console.getOut().println(str);
         
    }
     
  
   public String convertToDecimal(String stringToConvert) 
   {
       if (decSepa!=".") 
       {
        stringToConvert = stringToConvert.replace(".", decSepa);   
       }
       return stringToConvert;
   }
    
   public String getCell(int row, int column) {
        String str="";
        if (lang=="FR") 
        {
          str="L"+row+"C"+(column);
        }
        else if (lang =="EN")
        {
            str="R"+row+"C"+(column);
        }
        else if (lang=="GE")
        {
            str="R"+row+"K"+(column);
        }
        return str;
    }
 
}