package com.example.melloncoffeeapp.order;

import static com.example.melloncoffeeapp.MainActivity.basketProducts;
import static com.example.melloncoffeeapp.MainActivity.setListViewHeightBasedOnChildren;
import static com.example.melloncoffeeapp.MainActivity.totalBasketAmount;
import static com.example.melloncoffeeapp.MainActivity.totalBasketProducts;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.melloncoffeeapp.R;
import com.example.melloncoffeeapp.mellon.VASCallsArkeBusiness;
import com.example.melloncoffeeapp.mellon.VASResponseListener;

import org.json.JSONException;
import org.json.JSONObject;

import timber.log.Timber;

public class OrderDetailsActivity extends AppCompatActivity implements VASResponseListener {

    TextView totalAmountTV;
    Button paymentBtn ;
    ImageView backButtonImageView;
    private VASCallsArkeBusiness vas = null;
    private String response;
    ListView orderdetailsLV;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);

        setViews();
        setListeners();
        initVas();

        OrderProductAdapter listAdapter = new OrderProductAdapter(this,0, basketProducts);
        orderdetailsLV.setAdapter(listAdapter);
        setListViewHeightBasedOnChildren(orderdetailsLV);
    }

    public void initVas()
    {
        if(vas==null)
        {
            vas = new VASCallsArkeBusiness(this);
            vas.addListener(this);
        }
    }

    private void setViews() {
        paymentBtn = findViewById(R.id.paymentBtn);
        totalAmountTV = findViewById(R.id.totalAmountTV);
        totalAmountTV.setText(getString(R.string.application_currency)
                + String.format("%.2f",totalBasketAmount));
        orderdetailsLV = findViewById(R.id.orderdetailsLV);
        backButtonImageView = findViewById(R.id.backButtonImageView);
    }

    private void setListeners() {
        paymentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doSale();
            }
        });

        backButtonImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void responseReceived() {
        response = vas.getResponseData();
        Log.i("response JSON:" , response);
        JSONObject sourceObject;
        try {
            sourceObject  = new JSONObject(response);


            if(sourceObject.get("responseMessage").equals("Transaction succeeded")
                    ||sourceObject.get("responseMessage").equals("Επιτυχημένη συναλλαγή"))
            {
                totalBasketProducts = 0;
                totalBasketAmount = 0;
                basketProducts.clear();
                this.finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void doSale()
    {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String requestParameters=
                        String.format("{\"amount\":%.2f,\"orderNumber\":\"%s\",\"needAppPrinted\":%b}",
                                totalBasketAmount,"1",true);
                Timber.i("request JSON:"+requestParameters);
                vas.setSendData(requestParameters);
                vas.doTransaction("SALE");
            }
        };
        this.runOnUiThread(r);

    }
    public void updateData(double amount ){
        totalAmountTV.setText(getString(R.string.application_currency)
                + String.format("%.2f",amount));
    }

}