package com.friendvilla.OfferG;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;
    private boolean doubleBackToExitPressedOnce = false;
    private List<Object> mRecyclerViewItems,mRecyclerViewItem;
    private RelativeLayout mainLayout;
    private LinearLayout shimmerLayout,recyclerLayout;
    private SharedPreferences SP;
    private Dialog bottomDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SP = getSharedPreferences("Festy", Context.MODE_PRIVATE);
        if (SP.contains("ThemeMode") && SP.getString("ThemeMode","Night").equals("Night")) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        setContentView(R.layout.activity_main);
        shimmerLayout = findViewById(R.id.shimmerLayout);
        recyclerLayout = findViewById(R.id.RecyclerLayout);
        mainLayout = findViewById(R.id.mainLayout);
        recyclerView = findViewById(R.id.RecyclerView);
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        ImageView home = findViewById(R.id.home);
        ImageView video = findViewById(R.id.play);
        ImageView menu = findViewById(R.id.menu);
        ImageView notice = findViewById(R.id.notice);
        ImageView profile = findViewById(R.id.profile);
        SearchView searchView = findViewById(R.id.search);
        Toolbar toolbar = findViewById(R.id.ToolBar);
        setSupportActionBar(toolbar);
        Typeface tf = ResourcesCompat.getFont(getBaseContext(),R.font.solaiman_lipi);
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView searchText = (TextView) searchView.findViewById(id);
        searchText.setAllCaps(false);
        searchText.setTextSize(14);
        searchText.setTypeface(tf);
        tabLayout.addTab(tabLayout.newTab().setText("Home"));
        tabLayout.addTab(tabLayout.newTab().setText("Recharge Offers"));
        tabLayout.addTab(tabLayout.newTab().setText("Shopping Offers"));
        tabLayout.addTab(tabLayout.newTab().setText("App Offers"));
        tabLayout.addTab(tabLayout.newTab().setText("Special Offers"));
        tabLayout.addTab(tabLayout.newTab().setText("Other Offers"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mRecyclerViewItems.clear();
                recyclerLayout.setVisibility(View.GONE);
                shimmerLayout.setVisibility(View.VISIBLE);
                addMenuItemsFromJson();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        AdView adsview = findViewById(R.id.adView);
        MobileAds.initialize(this);
        AdRequest adRequest = new AdRequest.Builder().build();
        adsview.loadAd(adRequest);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        addMenuItemsFromJson();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!recyclerView.canScrollVertically(1)) {
                    addMoreItemsFromJson();
                }

            }
        });
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheet();
            }
        });
        notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent noticeIntent = new Intent(MainActivity.this,NotificationActivity.class);
                startActivity(noticeIntent);
            }
        });
    }

    private void showBottomSheet(){
        bottomDialog = new Dialog(this);
        bottomDialog.setContentView(R.layout.bottom_sheet);
        Window window = bottomDialog.getWindow();
        assert window != null;
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.setCancelable(true);
        LinearLayout Setting = bottomDialog.findViewById(R.id.setting);
        LinearLayout Shop = bottomDialog.findViewById(R.id.shop);
        LinearLayout Group = bottomDialog.findViewById(R.id.group);
        LinearLayout Theme = bottomDialog.findViewById(R.id.theme);
        LinearLayout Share = bottomDialog.findViewById(R.id.share);
        LinearLayout Contact = bottomDialog.findViewById(R.id.contact);
        LinearLayout Info = bottomDialog.findViewById(R.id.info);
        LinearLayout Exit = bottomDialog.findViewById(R.id.exit);
        LinearLayout Close = bottomDialog.findViewById(R.id.closeSheet);
        final ImageView icon = bottomDialog.findViewById(R.id.themeIcon);
        if (SP.contains("ThemeMode") && SP.getString("ThemeMode","Night").equals("Night")) {
            icon.setImageResource(R.drawable.ic_sun);
        } else {
            icon.setImageResource(R.drawable.moon);
        }
        Setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Theme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                SharedPreferences.Editor SPEdit = SP.edit();
                if (SP.contains("ThemeMode") && SP.getString("ThemeMode","Night").equals("Night")) {
                    SPEdit.putString("ThemeMode","Day");
                    SPEdit.apply();
                    Intent intent = getIntent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    finish();
                    startActivity(intent);
                } else {
                    SPEdit.putString("ThemeMode","Night");
                    SPEdit.commit();
                    Intent intent = getIntent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    finish();
                    startActivity(intent);
                }

            }
        });
        Contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                MainActivity.super.onBackPressed();
            }
        });
        Close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
            }
        });
        bottomDialog.show();
    }

    private void loadMenu() {
        shimmerLayout.setVisibility(View.INVISIBLE);
        recyclerLayout.setVisibility(View.VISIBLE);
    }

    private void addMenuItemsFromJson() {
        mRecyclerViewItems = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MenuItem menuItem = new MenuItem(String.valueOf(i),"news_heading","cat_id","news_status","news_date","news_image","news_description");
            mRecyclerViewItems.add(menuItem);
        }
        adapter = new RecyclerViewAdapter(MainActivity.this,mRecyclerViewItems);
        recyclerView.setAdapter(adapter);
        loadMenu();
    }

    private void addMoreItemsFromJson() {
        mRecyclerViewItem = new ArrayList<>();
        for (int i = 0; i <10; i++) {
            MenuItem menuItem = new MenuItem(String.valueOf(i),"news_heading","cat_id","news_status","news_date","news_image","news_description");
            mRecyclerViewItem.add(menuItem);
        }
        adapter.addMoreData(mRecyclerViewItem);
        Toast.makeText(MainActivity.this,"New Data Loaded",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        assert connectivityManager != null;
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    }

    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            MainActivity.super.onBackPressed();
            return;
        }
        this.doubleBackToExitPressedOnce = true;
        Snackbar.make(mainLayout,"Please click BACK again to exit", Snackbar.LENGTH_LONG).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }
}