package blockchain.breizhcamp.speakersdapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.dd.CircularProgressButton;
import com.sqli.blockchain.android_geth.EthereumService;

import ethereumjava.exception.EthereumJavaException;
import ethereumjava.module.objects.TransactionRequest;
import ethereumjava.solidity.SolidityUtils;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

public class WriteActivity extends AppCompatActivity implements EthereumService.EthereumServiceInterface{

    private static final int COMPLETE_STATE = 100;
    private static final int IDLE_STATE = 0;
    private static final int ERROR_STATE = -1;

    private static final String LOG_ID = WriteActivity.class.getSimpleName();

    ProgressBar progressBar;
    MyApplication application;
    CircularProgressButton sendMessageButton;
    EditText messageEdittext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_layout);

        application = (MyApplication) getApplication();
        application.registerGethReady(this);

        progressBar = (ProgressBar) findViewById(R.id.write_loading);
        progressBar.setIndeterminate(true);
        progressBar.setVisibility(ProgressBar.VISIBLE);

        sendMessageButton = (CircularProgressButton) findViewById(R.id.send_message_button);
        sendMessageButton.setEnabled(false);
        sendMessageButton.setOnClickListener(v -> sendButtonClicked());
        messageEdittext = (EditText) findViewById(R.id.message_edittext);

        messageEdittext.addTextChangedListener(new MessageTextWatcher());

    }

    private void sendButtonClicked(){
        if( sendMessageButton.isIndeterminateProgressMode() ){
            sendMessageButton.setProgress(IDLE_STATE);
            //TODO Remove
        } else {
            switch (sendMessageButton.getProgress()) {
                case ERROR_STATE:
                    sendMessageButton.setProgress(IDLE_STATE);
                    break;
                case IDLE_STATE:
                    try {
                        sendMessage(messageEdittext.getText().toString());
                    }catch(EthereumJavaException e){
                        sendMessageButton.setProgress(-1);
                        Log.e(LOG_ID,e.getMessage());
                    }
                    sendMessageButton.setIndeterminateProgressMode(true);
                    break;
                case COMPLETE_STATE:
                    sendMessageButton.setProgress(IDLE_STATE);
                    break;
                default:
                    break;
            }
        }
    }

    private void sendMessage(String message){
        String account = application.ethereumjava.personal.listAccounts().get(0);
        TransactionRequest tx = new TransactionRequest(account,account);
        tx.setDataHex(SolidityUtils.toHex(message));
        application.ethereumjava.eth.sendTransaction(tx);
        //TODO ethereumjava ajouter méthode de notification onMined
    }

    class MessageTextWatcher implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean enabled = false;
            if( s.length() > 0 ){
                enabled = true;
            }
            sendMessageButton.setEnabled(enabled);
            Log.d("APP","ENABLED : "+enabled);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    public void onEthereumServiceReady() {

            Observable.just(true)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(b -> progressBar.setVisibility(ProgressBar.INVISIBLE));


    }



}

@@ -1955,24 +1955,91 @@
 ssMode() )%7B%0A
+            sendMessageButton.setIndeterminateProgressMode(false);%0A
             

fix bug with loader in speakers-dapp on resume