package macampcorp.macamp.game;

import android.util.Log;

import com.google.zxing.common.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import macampcorp.macamp.utils.L;

public class Bomb implements Serializable{

    public  static final int BOMB_UNARMED=0;
    public  static final int BOMB_ARMED=1;
    public  static final int BOMB_DISARMED=2;
    public  static final int BOMB_EXPLOSED=3;

    protected long      mDeadLine;
    protected long      mDisarmAt=0;
    protected String     mPassword;
    protected char      mPasswordKnown[];
    protected int       mStatus;
    protected int       mMinutes;

    public Bomb(String pwd, int min)
    {
        mMinutes=min;
        mPassword=pwd;
        mPasswordKnown=new char[mPassword.length()];
        for(int i=0; i<mPassword.length(); i++)
            mPasswordKnown[i]='_';
        mDeadLine = Calendar.getInstance().getTimeInMillis()+min*60*1000;
        mStatus=BOMB_UNARMED;
    }


    public void arm()
    {
        mDeadLine = Calendar.getInstance().getTimeInMillis()+mMinutes*60*1000;
        mStatus=BOMB_ARMED;
    }

    public int getPasswordLength()
    {
        return mPassword.length();
    }

    public boolean disarm(String password)
    {
        if(password.toLowerCase().compareTo(mPassword.toLowerCase())==0)
        {
            mDisarmAt = (mDeadLine - Calendar.getInstance().getTimeInMillis()) / 1000;
            mStatus=BOMB_DISARMED;
            return true;
        }else {
            return false;
        }

    }


    public void addKnownCharacter(int pos)
    {
        L.debug("Bomb_debug","Adding character "+pos);
        if(pos<0) return;
        mPasswordKnown[pos]=mPassword.charAt(pos);
    }

    public String getPasswordKnown()
    {
        String s = "";
        for(int i=0; i<mPassword.length(); i++)
        {
            if(i>=0) s+=" ";
            s+=mPasswordKnown[i];
        }
        return s;
    }


    public void addTime(float minutes)
    {
        int x = (int)(minutes*60.0*1000);
        L.info("Ajout de temps : "+x+" secondes "+minutes);
        mDeadLine+=x;
    }

    public String getTimeBeforeBoom()
    {
        if(mStatus==BOMB_ARMED || mStatus==BOMB_DISARMED) {
            long now = 0;
            if(mStatus==BOMB_ARMED)
                now=(mDeadLine - Calendar.getInstance().getTimeInMillis()) / 1000;
            else
                now=mDisarmAt;


            int h = 0, m = 0, s = 0;
            String ret = "";

            s = (int) now % 60;
            m = (int) (now % 3600) / 60;
            h = (int) now / 3600;

            if (h > 0) {
                if (h < 10) ret += "0";
                ret += h;
                ret += ":";
            }

            if (m < 10) ret += "0";
            ret += m;

            ret += ":";

            if (s < 10) ret += "0";
            ret += s;

            return ret;
        }else
        {
            return "--:--:--";
        }
    }

    public long getSecondsBeforeBoom()
    {
        return (long)  (mDeadLine - Calendar.getInstance().getTimeInMillis())/1000;
    }

    public void setSecondsBeforeBoom(long s)
    {
        mDeadLine=(s*1000+Calendar.getInstance().getTimeInMillis());
    }

    protected void explose()
    {
        mStatus=BOMB_EXPLOSED;
    }

    public boolean hasExplosed()
    {

        if(getSecondsBeforeBoom()<0 && mStatus==BOMB_ARMED)
            explose();

        return mStatus==BOMB_EXPLOSED;
    }

    public int getStatus() { return mStatus; }
}
