package com.app.rewardapp.ui.fragments;

import static com.ads.androidsdk.sdk.util.Tool.isAppInstalled;
import static com.app.rewardapp.util.Fun.data;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
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.CallbackResp;
import com.app.rewardapp.Responsemodel.TaskResp;
import com.app.rewardapp.adapters.TaskAdapter;
import com.app.rewardapp.adsManager.RewardAds;
import com.app.rewardapp.databinding.AppdialogBinding;
import com.app.rewardapp.databinding.FragmentHomeBinding;
import com.app.rewardapp.listener.OnItemClickListener;
import com.app.rewardapp.restApi.ApiClient;
import com.app.rewardapp.restApi.ApiInterface;
import com.app.rewardapp.restApi.WebApi;
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 com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Task extends Fragment implements OnItemClickListener, AdImp {
    FragmentHomeBinding binding;
    List<TaskResp> list;
    AppdialogBinding appbind;
    Boolean isValidInstall = false,rewardMode=false;
    String packageSent = "", id;
    Activity activity;
    Session session;
    private AlertDialog app, loading, alert, bonus;
    TaskAdapter adapter;
    AdManager adManager;
    int item,posi;
    RewardAds.Builder adNetwork;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(getLayoutInflater());

        init();
        initDialog();
        initrv();
        getData();

        return binding.getRoot();
    }

    private void init() {
        binding.toolbar.setText(getString(R.string.activity_offers));

        session = new Session(getActivity());
        activity = getActivity();
        alert = Fun.Alert(activity);
        loading = Fun.loading(activity);
        adManager = new AdManager(activity);
        adNetwork = new RewardAds.Builder(activity);

        adManager.loadBannerAd(binding.getRoot());
        getActivity().findViewById(R.id.navigation).setVisibility(View.GONE);
        loadReward();

        binding.getRoot().setFocusableInTouchMode(true);
        binding.getRoot().requestFocus();
        binding.getRoot().setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                FragmentMain NameofFragment = new FragmentMain();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.container, NameofFragment);
                transaction.commit();
                return true;
            }
            return false;
        });

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


    }

    private void initDialog() {
        bonus = new AlertDialog.Builder(activity).setView(LayoutInflater.from(activity).inflate(R.layout.layout_collect_bonus, null)).create();
        bonus.getWindow().setBackgroundDrawableResource(R.color.transparent);
        bonus.getWindow().setWindowAnimations(R.style.Dialoganimation);
        bonus.setCanceledOnTouchOutside(false);

        appbind = AppdialogBinding.inflate(getLayoutInflater());
        app = new AlertDialog.Builder(activity).setView(appbind.getRoot()).create();
        app.getWindow().setBackgroundDrawableResource(R.color.transparent);
        app.getWindow().setWindowAnimations(R.style.Dialoganimation);
        app.setCanceledOnTouchOutside(false);
    }

    void initrv() {
        list = new ArrayList<>();
        binding.recyclerViewApps.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new TaskAdapter(list, getActivity());
        adapter.setClickListener(this);
        binding.recyclerViewApps.setAdapter(adapter);
    }

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

    void showbonus(String msg, String type) {
        bonus.show();
        TextView tv, congrats;
        tv = bonus.findViewById(R.id.txt);
        congrats = bonus.findViewById(R.id.congrts);
        tv.setText(msg);
        if (type.equals("error")) {
            congrats.setText(getString(R.string.oops));
            congrats.setTextColor(getResources().getColor(R.color.red));
        }
        bonus.findViewById(R.id.closebtn).setOnClickListener(view -> {
            bonus.dismiss();
        });
    }

    void showAlert(String msg) {
        alert.show();
        TextView tv = alert.findViewById(R.id.txt);
        tv.setText(msg);
        alert.findViewById(R.id.close).setOnClickListener(v -> {
            alert.dismiss();
        });
    }

    private void showDialog() {
        loading.show();
    }

    private void dismissDialog() {
        if (loading.isShowing()) {
            loading.dismiss();
        }
    }

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

    private void installApp(String appId) {
        showDialog();
        ApiClient.getClient(getActivity()).create(ApiInterface.class).Api(data("","","","","","",11,Integer.parseInt(id),session.Auth())).enqueue(new Callback<CallbackResp>() {
            @Override
            public void onResponse(Call<CallbackResp> call, Response<CallbackResp> response) {
                dismissDialog();
                if (response.isSuccessful() && response.body().getCode() != 0) {
                    session.setIntData(session.WALLET,response.body().getBalance());
                    removeItem(posi);
                    showbonus(response.body().getMsg(), "");
                    isValidInstall = false;
                    packageSent = "";
                } else {
                    try {
                        showbonus(response.body().getMsg(), "error");
                    }catch (Exception e){}
                }
            }

            @Override
            public void onFailure(Call<CallbackResp> call, Throwable t) {
                dismissDialog();
            }
        });
    }

    private void showDownloadInfo() {
        app.show();
        Glide.with(getActivity()).load(WebApi.Api.IMAGES + list.get(posi).getImage()).placeholder(R.drawable.placeholder).into(appbind.images);
        appbind.tvTitle.setText(list.get(posi).getAppName());
        appbind.desc.setText(Html.fromHtml(list.get(posi).getDetails()));

        appbind.startoffer.setOnClickListener(view -> {
            isValidInstall = true;
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(list.get(posi).getAppurl()));
            startActivity(intent);

        });

        appbind.close.setOnClickListener(v -> {
            app.dismiss();
        });

    }

    @Override
    public void onResume() {
        if (isValidInstall) {
            if (isAppInstalled(getActivity(),list.get(posi).getUrl())) {
                if(app.isShowing()){ app.dismiss();}
                rewardMode=true;
                adNetwork.showReward();
            }else {
                if(app.isShowing()){ app.dismiss();}
                Toast.makeText(activity, getString(R.string.msg_not_complete), Toast.LENGTH_SHORT).show();
                adNetwork.showReward();
            }
        }

        if(rewardMode) {
            if (adNetwork.isCompleted()) {
                adNetwork.setCompleted(false);
                adNetwork.buildAd();
                installApp(id);
            }
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void getData() {
        ApiClient.getClient(getActivity()).create(ApiInterface.class).ApiTask(Fun.data("","","","","","",5,0,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 displayData(Response<List<TaskResp>> response) {
        for (int i = 0; i < response.body().size(); i++) {
            if (isAppInstalled(getActivity(),response.body().get(i).getUrl())) {

            }else{
                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 onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View view, int position) {
        id = list.get(position).getId();
        this.posi=position;
        showDownloadInfo();
    }

    private void loadReward() {
        adNetwork= new RewardAds.Builder(activity);
        adNetwork.setAdcolony_Appid(adcolonyAppID);
        adNetwork.setAdcolonyZone(adcolonyZoneID);
        adNetwork.setAdmob_au(admobRewardID);
        adNetwork.setMax_au(applovinRewardID);
        adNetwork.setUnity_au(unityRewardID);
        adNetwork.setAdmob_reward(ADMOB_REWARD);
        adNetwork.setAdcolony_reward(ADCOLONY_REWARD);
        adNetwork.setApplovin_reward(APPLOVIN_REWARD);
        adNetwork.setStatartapp_reward(STARTAPP_REWARD);
        adNetwork.setUnity_reward(UNITY_REWARD);
        adNetwork.setRewardMode(true);
        adNetwork.buildAd();
    }

    private void removeItem(int posi){
        list.remove(posi);
        adapter.notifyDataSetChanged();

        if(list.size()<5){
            list.clear();
            getData();
        }
    }
}