package com.cygnusa.hyperm;

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.wrappers.ErrorResponse;
import com.cygnusa.hyperm.wrappers.QrCodeResponse;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShowQrCodeClass extends AppCompatActivity {

    TextView txtTrainingDate, txtTrainingTitle, txtTrainingDuration;
    ImageView imgScan;
    private ApiService apiService;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setContentInsetStartWithNavigation(0);
        toolbar.setTitle("");
        Objects.requireNonNull(getSupportActionBar()).setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        @SuppressLint("PrivateResource")
        Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
        backArrow.setColorFilter(getResources().getColor(R.color.btn_bg_color), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(backArrow);

        initViews();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        this.overridePendingTransition(R.anim.animation_f_enter,
                R.anim.animation_f_leave);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initViews() {
        txtTrainingDate = findViewById(R.id.txtTrainingDate);
        txtTrainingTitle = findViewById(R.id.txtTrainingTitle);
        txtTrainingDuration = findViewById(R.id.txtTrainingDuration);
        apiService = ((MainApplication) getApplication()).getClient();
        imgScan = findViewById(R.id.imgQR);

        getQrCodeData();

        //GlobalData.mSingleDataLists.getScheduleId()
    }

    private void getQrCodeData() {
        apiService.getQrCodeImage(GlobalData.user.getData().getSmpin()).enqueue(new Callback<QrCodeResponse>() {
            @Override
            public void onResponse(Call<QrCodeResponse> call, Response<QrCodeResponse> response) {
                if (response.body() != null) {
                    if (response.body().getMessage().equalsIgnoreCase("Success")) {
                        if (response.body().getData() != null) {
                            Glide.with(ShowQrCodeClass.this).load(response.body().getData().getLink())
                                    .thumbnail(Glide.with(ShowQrCodeClass.this).load(R.drawable.placeholder))
                                    .apply(new RequestOptions().fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL)
                                            .placeholder(R.drawable.placeholder)).into(imgScan);

                            txtTrainingDate.setText(response.body().getData().getStartDate());
                            txtTrainingTitle.setText(response.body().getData().getTitle());
                            txtTrainingDuration.setText(String.valueOf(response.body().getData().getDuration()));
                        }
                    } else {
                        GlobalData.toast(ShowQrCodeClass.this, response.body().getResponseText());
                    }
                } else if (response.body() != null && response.body().getStatus() == 401) {
                    GlobalData.canLogout = true;
                    finish();
                } else if (response.errorBody() != null) {
                    Gson gson = new Gson();
                    ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                    GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
                    if (message.getStatus() == 401) {
                        GlobalData.canLogout = true;
                        finish();
                    }
                }
            }

            @Override
            public void onFailure(Call<QrCodeResponse> call, Throwable t) {
                if (t.getMessage() != null) {
                    Log.e("Tmeaasge", t.getMessage());
                }
            }
        });
    }
}


