package com.cygnusa.hyperm.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.cygnusa.hyperm.LoginActivity;
import com.cygnusa.hyperm.ProductVideoViewActivity;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.adapter.ProductVideoAdapter;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.CommonMethods;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.util.StorePreference;
import com.cygnusa.hyperm.wrappers.ErrorResponse;
import com.cygnusa.hyperm.wrappers.ProductVideoResponse;
import com.cygnusa.hyperm.wrappers.ProductVideos;

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

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

public class ProductVideosFragment extends Fragment implements View.OnClickListener {

    public static ProductVideos content = new ProductVideos();
    ApiService restService;
    StorePreference storePreference;
    ImageView imgBack;
    View rootView;
    int PAGE = 0;
    int LIMIT = 10;
    ArrayList<ProductVideos> listData = new ArrayList<>();
    TextView textNoData;
    private GridView gridView;
    private Activity activity;
    private ProductVideoAdapter productAdapter;
    private ProgressBar progressBar;
    private String modelId;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_product_videos, null);
        activity = getActivity();
        storePreference = new StorePreference(activity);
        progressBar = rootView.findViewById(R.id.progress);
        gridView = rootView.findViewById(R.id.gridView);
        restService = ((MainApplication) activity.getApplication()).getClient();
        TextView txtName = rootView.findViewById(R.id.textView);
        imgBack = rootView.findViewById(R.id.logo);
        textNoData = rootView.findViewById(R.id.noDataFound);
        textNoData.setVisibility(View.GONE);
        txtName.setText(storePreference.getStringValue(GlobalData.SELECTED_MODEL_NAME));
        imgBack.setOnClickListener(this);
        modelId = storePreference.getStringValue(GlobalData.SELECTED_MODEL);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                ProductVideoViewActivity.CONTENT = listData.get(position);
                Intent intent = new Intent(getActivity(), ProductVideoViewActivity.class);
                storePreference.setInt(GlobalData.SELECTID, position);
                startActivity(intent);
            }
        });
        if (CommonMethods.isNetworkAvailable(getActivity())) {
            getAllProductVideos();
        }
        return rootView;
    }

    private void getAllProductVideos() {
        progressBar.setVisibility(View.VISIBLE);
        restService.getProductVideos(modelId, PAGE, LIMIT).enqueue(new Callback<ProductVideoResponse>() {
            @Override
            public void onResponse(Call<ProductVideoResponse> call, Response<ProductVideoResponse> response) {
                progressBar.setVisibility(View.GONE);
                if (response.body() != null && response.body().getStatus() == 200) {
                    listData = response.body().getData();
                    if (listData != null && listData.size() > 0) {
                        textNoData.setVisibility(View.GONE);
                        setVideoList(listData);
                    } else {
                        textNoData.setVisibility(View.VISIBLE);
                        textNoData.setText("No video available");
                    }
                } else if (response.body() != null && response.body().getStatus() == 401) {
                    GlobalData.canLogout = true;
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    Objects.requireNonNull(getActivity()).finish();
                } else if (response.errorBody() != null) {
                    Gson gson = new Gson();
                    ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                    //GlobalData.showToast(Objects.requireNonNull(getActivity()), message.getMessage(), Toast.LENGTH_SHORT).show();
                    if (message.getStatus() == 401) {
                        GlobalData.canLogout = true;
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                        Objects.requireNonNull(getActivity()).finish();
                    }
                }
            }

            @Override
            public void onFailure(Call<ProductVideoResponse> call, Throwable t) {
//                progressBar.setVisibility(View.GONE);
                if (t.getMessage() != null) {
                    Log.e("Server Error", t.getMessage());
                }
            }
        });

    }

    private void setVideoList(ArrayList<ProductVideos> productVideosData) {
        productAdapter = new ProductVideoAdapter(getContext(), productVideosData);
        gridView.setAdapter(productAdapter);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logo:
                activity.onBackPressed();
                break;

            default:
        }
    }

}