package com.yono.messeripos;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.material.appbar.MaterialToolbar;
import com.yono.messeripos.adapter.TransactionAdapter;
import com.yono.messeripos.models.MainViewModels;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class HistoryActivity extends AppCompatActivity {

    TransactionAdapter transactionAdapter;
    PaymentsModels paymentsModels;
    MainViewModels mainViewModels;
    RecyclerView recyclerView;
    Utils utils = new Utils();
    ProgressBar loading;

    public static final String DATA_TRANSACTION_PENDING = "DATA_TRANSACTION_PENDING";

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

        MaterialToolbar toolbars = findViewById(R.id.cAppBar);
        recyclerView = findViewById(R.id.rvHistory);
        loading = findViewById(R.id.loading);

        loading.setVisibility(View.VISIBLE);
        recyclerView.addItemDecoration(utils.dividerItemDecoration(HistoryActivity.this));
        transactionAdapter = new TransactionAdapter();
        mainViewModels = new ViewModelProvider(this).get(MainViewModels.class);

        setSupportActionBar(toolbars);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("All Transaction");
        toolbars.setNavigationOnClickListener(view -> onBackPressed());



        mainViewModels.getAllTransactions().observe(HistoryActivity.this,
                new Observer<DataResponse<List<PaymentsModels<String>>>>() {
                    @Override
                    public void onChanged(DataResponse<List<PaymentsModels<String>>> listDataResponse) {
                        Log.i("Hasil_transaksi", "onChanged: "+utils.convertGson(listDataResponse.getData()));
                        ArrayList<PaymentsModels> newData = new ArrayList<>();
                        newData.addAll(listDataResponse.getData());
                        transactionAdapter.setTransactionAdapter(HistoryActivity.this, newData);

                        loading.setVisibility(View.GONE);

                        recyclerView.setLayoutManager(new LinearLayoutManager(HistoryActivity.this,RecyclerView.VERTICAL,false));
                        recyclerView.setAdapter(transactionAdapter);
                    }
                });

        transactionAdapter.setListener(new TransactionAdapter.TransactionListener() {
            @Override
            public void onShowDetail(PaymentsModels paymentsModels) {
                if (paymentsModels.getPaymentType().equals("bank_transfer")
                        && paymentsModels.getTransactionStatus().equals("settlement")){
                    new AlertDialog.Builder(HistoryActivity.this)
                            .setTitle("Transaction")
                            .setMessage("Transaction success!").show();
                }else if (paymentsModels.getPaymentType().equals("bank_transfer")
                        && paymentsModels.getTransactionStatus().equals("pending")){
                    new AlertDialog.Builder(HistoryActivity.this)
                            .setTitle("Transaction")
                            .setMessage("Waiting payment!").show();
                }else if (paymentsModels.getPaymentType().equals("cash")){
                    new AlertDialog.Builder(HistoryActivity.this)
                            .setTitle("Transaction")
                            .setMessage("Transaction success!").show();
                }else if (paymentsModels.getPaymentType().equals("pending")){
                    Log.i("JAJSKASA", "onShowDetail: "+utils.convertGson(paymentsModels));
                    startActivity(new Intent(HistoryActivity.this,
                            CartActivity.class).putExtra(DATA_TRANSACTION_PENDING, paymentsModels));
                }
            }
        });

    }
}