package lt.ktu.mockus.srgm;

import java.util.ArrayList;

public class SRGMCustomer {
	public StockContainer dZpred  = null;//new double[StartupApplet.MAX_STOCK_COUNT];
	public double dProfit = 0;
        public StockContainer currentPrice = null;
        public ArrayList iN = null;
	public int iIterN = 0;
	public int iStrategy = 0;
        public int riskStrategy = 0;
	public double Delta  = 0;
        
        public int InvestmentType = 0;
        public int InvestmentForShortStrategies = 0;
        public int InvestmentInterval = 0;
        public int InvestmentMetod = 0;
        public int InvestmentIterations = 1000;
        //customers own funds
        public double C0_ = 0; //500
        public double C0 = C0_;
        //customers kredit limit
        public double Limit = 100; //5000
        //borrowed sum
        public double B = 0;
        public boolean insolvement = false;
        public double forcebuy = 1.0d;
        public double forcesell = 1.0d;
        public boolean insolvementCounted = false;
        
        public int stockCount = 3;
        
        public boolean longInvest = false;
        public int longInvestmentIteration = 0;
        
        //public int RiskStrategy = 1;
                
        public SRGMCustomer( int stockC ) {
            stockCount = stockC;
        }
        
        public void BuyStocks( int stockIndex, int nb, double price ) {
            int stockC = ((Integer)iN.get(stockIndex)).intValue() + nb;
            iN.set( stockIndex, stockC );
            //cia perkam uz visus pinigus, todel pirmas netinka
            if ( nb <=  C0 / price ) {
                C0 -= nb * price;
            }
            else {
                double sum = nb * price;
                sum -= C0;
                C0 = 0;
                B += sum;
            }
        }
        
        public void SellStocks( int stockIndex, int ns, double price ) {
            int stockC = ((Integer)iN.get(stockIndex)).intValue();
            C0 += price * ns ;
            stockC = stockC - ns;
            iN.set( stockIndex, stockC );
        }
        
        public void SellAllStocksByIndex( int stockIndex, double price ) {
            C0 += price * ((Integer)iN.get(stockIndex)).intValue();
            iN.set(stockIndex, 0);
        }
        
        public void CheckInsolvement() {
            if ( insolvementCounted ) {
                insolvement = true;
            }
            else {
                double stockMoney = 0;
                //System.out.println( "current size " + currentPrice.size() );
                for ( int i = 0; i < currentPrice.size(); i++ ) {
                    //System.out.println( "tut i " + i );
                    int in = ((Integer)iN.get(i)).intValue();
                    stockMoney += in * ((Double)currentPrice.get(i)).doubleValue();
                }
                insolvement = ( Limit <= ( B - stockMoney - C0 ) );
            }
        }
        
        public double PayInsolvement() {
            if ( ! insolvement ) return 0;
            double stockMoney = 0;
            for ( int i = 0; i < currentPrice.size(); i++ ) {
                int in = ((Integer)iN.get(i)).intValue();
                double curPr  = ((Double)currentPrice.get(i)).doubleValue();
                stockMoney += in * curPr;
                //stockMoney += iN[i] * currentPrice[i];
            }
            double rez = B - ( stockMoney );
            B = 0;
            Limit = 0;
            C0 = 0;
            for ( int i = 0; i < iN.size(); i++ ) {
                iN.set(i, 0);
            }
            insolvementCounted = true;
            return rez;
        }
        
        public double BankMinus() {
            return B;
        }
        
	public double getAvFunds() {
            CheckInsolvement();
            if ( insolvement ) return 0;
            //System.out.println( " avaliable funds C = " + C0 + " + " + " (  " + Limit + " - " + B + " ) " );
            double rezult = C0 + ( Limit - B );
            if ( rezult >= 0 ) return rezult;
            else return 0;
        }         

        public boolean PayCredit() {
            CheckInsolvement();
            if ( C0 <= 0 || B <= 0  )  return true;
            //System.out.println( " pries grazinima vartotojas turi grynu " + C0 + " ir kredita " + B );
            if (  C0 > B ) {
                C0 = C0 - B;
                B = 0;
                //System.out.println( "po grazinimo vartotojas turi grynu " + C0 + " ir kredita " + B );
                return true;
            }
            else {
                B = B - C0;
                C0 = 0;
                //System.out.println( "po grazinimo vartotojas turi grynu " + C0 + " ir kredita " + B );
                return true;
            }
        }
                
	public void prepareStorage() {
            iN = new ArrayList();
            currentPrice = new StockContainer();
            dZpred = new StockContainer();
            for( int c = 0; c < stockCount; c++ ) {
                dZpred.add(c, 0);
                iN.add( c, 0 );
                currentPrice.add( c, 0.0 );
            }
            
            
	}
           public void setInitialData( double dZpred[], double dProfit, int iN[], int iIterN, double Delta, double maxCredit) {
            for( int c = 0; c < stockCount; c++ ) {
                this.dZpred.add(c, dZpred[c]);
                this.iN.add(c, iN[c]);
            }
            this.iIterN = iIterN;
            this.dProfit = dProfit;
            this.Delta = Delta;
	}
        
        @Override
	public String toString(){
            return "dZpred:" + this.dZpred + " dProfit:" + this.dProfit + " iN:" + this.iN + " iIterN:" + this.iIterN + " iStrategy:" + this.iStrategy;
	}
}
