package com.app.rewardapp.ui.fragments;

import static com.ads.androidsdk.sdk.util.Tool.coolNumberFormat;
import static com.app.rewardapp.util.Fun.data;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.app.rewardapp.Config.Config;
import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.CallbackResp;
import com.app.rewardapp.adapters.ViewpagerAdapter;
import com.app.rewardapp.databinding.FragmentMainBinding;
import com.app.rewardapp.databinding.LayoutLanguageBinding;
import com.app.rewardapp.restApi.ApiClient;
import com.app.rewardapp.restApi.ApiInterface;
import com.app.rewardapp.ui.activity.ClaimBonus;
import com.app.rewardapp.ui.activity.Splash;
import com.app.rewardapp.util.Fun;
import com.app.rewardapp.util.Session;
import com.bumptech.glide.Glide;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.tabs.TabLayout;

import java.util.Locale;

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

public class FragmentMain extends Fragment {
    FragmentMainBinding binding;
    private ViewpagerAdapter catadapter;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    Session session;
    boolean doubleBackToExitPressedOnce = false;
    BottomSheetDialog bottomSheetDialog;
    LayoutLanguageBinding langBind;
    private AlertDialog loading;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding=FragmentMainBinding.inflate(getLayoutInflater(),container,false);

        session=new Session(getActivity());
        getActivity().findViewById(R.id.navigation).setVisibility(View.VISIBLE);
        loading = Fun.loading(getActivity());

        if(Config.ENABLE_LANGUAGE){
            if(session.getData(session.SELECTED_LANGUAGE).equalsIgnoreCase("")){
                chooseLang();
            }
        }

        System.out.println("resp___"+session.getIntData(session.from_refer));
        if(session.getIntData(session.from_refer)>0){
            binding.claimbonus.setVisibility(View.GONE);
        }

        binding.claimbonus.setOnClickListener(v -> {
            startActivity(new Intent(getActivity(), ClaimBonus.class));
        });

        binding.translate.setOnClickListener(v -> {
            chooseLang();
        });

        viewPager = binding.catviewpager;
        tabLayout= binding.tablayout;
        catadapter= new ViewpagerAdapter(getChildFragmentManager());
        catadapter.AddFragment(new HomeNew(),"test");
        catadapter.AddFragment(new Games(),"test");
        viewPager.setAdapter(catadapter);
        viewPager.setOffscreenPageLimit(1);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText(getString(R.string.Top_picks));
        tabLayout.getTabAt(1).setText(getString(R.string.play_zone));


        binding.getRoot().setFocusableInTouchMode(true);
        binding.getRoot().requestFocus();
        binding.getRoot().setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if(viewPager.getCurrentItem()>0){
                    viewPager.setCurrentItem(0);
                }else if(viewPager.getCurrentItem()==0) {
                    if(doubleBackToExitPressedOnce){
                        if(session.getBoolean("exit")){
                            Intent a = new Intent(Intent.ACTION_MAIN);
                            a.addCategory(Intent.CATEGORY_HOME);
                            a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(a);
                        }else {
                            RatingDialogFragment dialog = RatingDialogFragment.newInstance();
                            dialog.show(getActivity().getSupportFragmentManager(), "rating_dialog");
                        }
                    }else {
                        doubleBackToExitPressedOnce=true;
                    }
                }
            }
            return false;
        });

        binding.refresh.setOnClickListener(v -> {
            binding.refresh.setEnabled(false);
            Glide.with(getActivity()).asGif().load(R.drawable.loading).into(binding.refresh);
            reload_coin();
        });

        binding.layoutCoin.setOnClickListener(v -> {
           loadFragment(new Coins());
        });

        return binding.getRoot();
    }


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

    @Override
    public void onResume() {
        binding.coins.setText(coolNumberFormat(Long.parseLong(String.valueOf(session.getIntData(session.WALLET)))));
        super.onResume();
    }


    private void reload_coin() {
        ApiClient.getClient(getActivity()).create(ApiInterface.class).ApiUser(data("","","","","","",15,0,session.Auth())).enqueue(new Callback<CallbackResp>() {
            @Override
            public void onResponse(Call<CallbackResp> call, Response<CallbackResp> response) {
                if(response.isSuccessful()){
                    try {
                        binding.coins.setText(coolNumberFormat(Long.parseLong(String.valueOf(response.body().getBalance()))));
                        session.setIntData(session.WALLET,response.body().getBalance());
                        Toast.makeText(getActivity(), "Coin Updated", Toast.LENGTH_SHORT).show();
                        new Handler().postDelayed(() -> binding.refresh.setEnabled(true),5000);
                        binding.refresh.setImageResource(R.drawable.ic_baseline_autorenew_24);

                    }catch (Exception e){

                    }
                }
            }

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

            }
        });
    }

    private void chooseLang() {
        bottomSheetDialog = new BottomSheetDialog(getActivity());
        langBind= LayoutLanguageBinding.inflate(getLayoutInflater());
        bottomSheetDialog.setContentView(langBind.getRoot());
        bottomSheetDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        bottomSheetDialog.show();

        if(!Config.ENGLISH){ langBind.en.setVisibility(View.GONE); }
        if(!Config.HINDI){ langBind.hi.setVisibility(View.GONE); }
        if(!Config.SPANISH){ langBind.es.setVisibility(View.GONE); }
        if(!Config.TURKEY){ langBind.tr.setVisibility(View.GONE); }
        if(!Config.BENGALI){ langBind.ben.setVisibility(View.GONE); }

        langBind.en.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
            setLocale("en");
        });

        langBind.hi.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
            setLocale("hi");
        });

        langBind.tr.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
            setLocale("tr");
        });

        langBind.ben.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
            setLocale("ben");
        });

        langBind.es.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
            setLocale("es");
        });

        langBind.close.setOnClickListener(v -> {
            session.setData(session.SELECTED_LANGUAGE,"en");
            bottomSheetDialog.dismiss();
        });

    }

    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        session.setData(session.SELECTED_LANGUAGE,lang);
        startActivity(new Intent(getActivity(), Splash.class));
    }

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

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

}