package mobos.leoss.si.qsystemmobileapp;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.OnClick;
import mobos.leoss.si.qsystemmobileapp.Comunnication.WebService;
import mobos.leoss.si.qsystemmobileapp.SupportClasses.CommonFunctions;
import mobos.leoss.si.qsystemmobileapp.interfaces.RestInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class QSystemMainActivity extends AppCompatActivity {

    Activity _activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qsystem_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        _activity = this;

        ButterKnife.bind(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_qsystem_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.btn_generate_new_number})
    public void generateClick(View view) {
        requestGetDocumentType(1);
    }

    //region REST requests
    private void requestGetDocumentType(int IdPrintAction) {
        Call<Integer> call = WebService.createService(RestInterface.class, "http://212.103.154.122/QSystemWS/QSystemService.svc/", _activity)
                .generateNewNumberForPrintAction(IdPrintAction);

        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                //Sporoči BusinessLogic napako, če je do nje prišlo, drugače nadaljujemo z obdelavo response-a
                if (!CommonFunctions.requestIsErrorJson(_activity, response) && response.body() != null) {
                    //Če gre za tovorni list potem pokličem preverjanje ustreznosti količine in šele nato povežem dokument s picking dokumentom
                    Toast.makeText(_activity, String.format("Strežnik je odgovoril: %s", response.body()), Toast.LENGTH_LONG ).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                //Kadar je strežnik nedosegljiv iz kakršnegakoli razloga se request konča z onFailure
                Toast.makeText(_activity, String.format("Zgodila se je napaka: %s", t.toString()), Toast.LENGTH_LONG ).show();
            }
        });
    }
    //endregion REST requests
}
