package com.app.rewardapp.ui.fragments;

import static com.ads.androidsdk.sdk.util.Tool.isConnected;
import static com.app.rewardapp.util.Constant_Api.Pos;
import static com.app.rewardapp.util.Constant_Api.REMOVE;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.TaskResp;
import com.app.rewardapp.adapters.TaskAdapter;
import com.app.rewardapp.databinding.FragmentVideoBinding;
import com.app.rewardapp.listener.OnItemClickListener;
import com.app.rewardapp.restApi.ApiClient;
import com.app.rewardapp.restApi.ApiInterface;
import com.app.rewardapp.ui.activity.YTVideoActivity;
import com.app.rewardapp.adsManager.AdImp;
import com.app.rewardapp.adsManager.AdManager;
import com.app.rewardapp.util.Fun;
import com.app.rewardapp.util.Session;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Video extends Fragment implements AdImp, OnItemClickListener {
    FragmentVideoBinding binding;
    Activity activity;
    Session session;
    List<TaskResp> list;
    TaskAdapter adapter;
    int item = 0;
    AdManager adManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentVideoBinding.inflate(getLayoutInflater());
        session = new Session(getActivity());
        activity = getActivity();
        adManager = new AdManager(activity);
        adManager.loadBannerAd(binding.getRoot());

        getActivity().findViewById(R.id.navigation).setVisibility(View.GONE);
        binding.tool.toolbar.setText(getString(R.string.activity_videozone));

        list = new ArrayList<>();
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new TaskAdapter(list, getActivity());
        adapter.setClickListener(this);
        binding.recyclerview.setAdapter(adapter);

        binding.tool.back.setOnClickListener(v -> {
            load_fragment(new FragmentMain());
        });

        binding.getRoot().setFocusableInTouchMode(true);
        binding.getRoot().requestFocus();
        binding.getRoot().setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                load_fragment(new FragmentMain());
                return true;
            }
            return false;
        });

        if (isConnected(getActivity())) {
            getdata();
        } else {
            Fun.Error(getActivity(), getString(R.string.no_internet));
        }

        return binding.getRoot();
    }

    private void load_fragment(Fragment fragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.commit();
    }

    private void getdata() {
       ApiClient.getClient(getActivity()).create(ApiInterface.class).ApiTask(Fun.data("","","","","","",6,2,session.Auth())).enqueue(new Callback<List<TaskResp>>() {
            @Override
            public void onResponse(Call<List<TaskResp>> call, Response<List<TaskResp>> response) {
                if (response.isSuccessful() && response.body().size() > 0) {
                    showItem(true);
                    displayData(response);
                } else {
                    showItem(false);
                }
            }

            @Override
            public void onFailure(Call<List<TaskResp>> call, Throwable t) {
                showItem(false);
            }
        });
    }

    private void showItem(boolean item) {
        if(item){
            binding.shimmerViewContainer.setVisibility(View.GONE);
            binding.recyclerview.setVisibility(View.VISIBLE);
        }else {
            binding.shimmerViewContainer.setVisibility(View.GONE);
            binding.noResult.setVisibility(View.VISIBLE);
        }
    }

    private void displayData(Response<List<TaskResp>> response) {
        for (int i = 0; i < response.body().size(); i++) {
            list.add(response.body().get(i));
            item++;
            if (item == nativeItem) {
                item = 0;
                switch (nativeType) {
                    case FB:
                        list.add(new TaskResp().setViewType(3));
                        break;
                    case ADMOB:
                        list.add(new TaskResp().setViewType(4));
                        break;
                    case STARTAPP:
                        list.add(new TaskResp().setViewType(5));
                        break;
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        if(REMOVE){
            removeItem(Pos);
        }
        super.onResume();
    }

    private void removeItem(int posi){
        list.remove(posi);
        adapter.notifyDataSetChanged();
        Pos=0;
        REMOVE=false;
        if(list.size()<5){
            list.clear();
            getdata();
        }
    }

    @Override
    public void onClick(View view, int position) {
        Pos=position;
        Intent go = new Intent(activity, YTVideoActivity.class);
        go.putExtra("video_id",list.get(position).getVideoId());
        go.putExtra("timer",list.get(position).getTimer());
        go.putExtra("point",list.get(position).getPoint());
        go.putExtra("id",list.get(position).getId());
        startActivity(go);
    }
}