package com.SpinMoney.SpinMoney;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.RecyclerView;

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

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;

public class SidebarAdapter extends RecyclerView.Adapter<SidebarAdapter.UserViewHolder> {

    private final List<String> titleList;
    private final List<String> subtitleList;
    private final List<Integer> iconList;
    private final DrawerLayout drawer;
    private final Context context;

    public SidebarAdapter(Context ctx, DrawerLayout drawer) {
        this.context = ctx;
        this.drawer = drawer;
        this.titleList = new ArrayList<>();
        this.iconList = new ArrayList<>();
        this.subtitleList = new ArrayList<>();
        titleList.add("Privacy Policy");titleList.add("Update App");titleList.add("Share App");titleList.add("Contact Us");titleList.add("Rate Us");titleList.add("Refund Policy");titleList.add("About Us");
        iconList.add(R.drawable.lock);iconList.add(R.drawable.update);iconList.add(R.drawable.share);iconList.add(R.drawable.email);iconList.add(R.drawable.rate);iconList.add(R.drawable.more);iconList.add(R.drawable.info);
        subtitleList.add("Our Terms & Policies");subtitleList.add("Checking For Update");subtitleList.add("Help us to build big family");subtitleList.add("Share your thought with us");subtitleList.add("Give your rate and feedback");subtitleList.add("How Refund Policy Works");subtitleList.add("About us");
    }

    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.lsv_menu,parent,false);
        return new UserViewHolder(view);
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, final int position) {
        holder.image.setImageResource(iconList.get(position));
        holder.title.setText(titleList.get(position));
        holder.sub_title.setText(subtitleList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                performClick(context,drawer,titleList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return titleList.size();
    }

    public static class UserViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;
        TextView sub_title;
        public UserViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.title);
            sub_title = itemView.findViewById(R.id.sub_title);
        }
    }

    private static void performClick(Context ctx, DrawerLayout drawer, String title){
        drawer.closeDrawer(GravityCompat.START);
        switch (title) {
            case "Privacy Policy": {
                Intent policy = new Intent(Intent.ACTION_VIEW);
                policy.setData(Uri.parse("https://azzurteam.blogspot.com/2020/12/privacy-policy-this-page-is-used-to.html"));
                ctx.startActivity(policy);
                break;
            }
            case "Update App": {
                UpdateChecker appUpdateChecker=new UpdateChecker((Activity) ctx);
                appUpdateChecker.checkForUpdate(true);
                break;
            }
            case "Share App": {
                try {
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("text/plain");
                    share.putExtra(Intent.EXTRA_SUBJECT,ctx.getResources().getString(R.string.app_name));
                    String msg = "\nLet me recommend you this application\n\nhttp://spinorreward.xyz";
                    share.putExtra(Intent.EXTRA_TEXT, msg);
                    ctx.startActivity(Intent.createChooser(share, "Share Via"));
                } catch(Exception e) {
                    e.fillInStackTrace();
                }
                break;
            }
            case "Contact Us": {
                String[] TO = {"contact@spinorreward.xyz"};
                String[] CC = {"contact@spinorreward.xyz"};
                Intent email = new Intent(Intent.ACTION_SEND);
                email.setDataAndType(Uri.parse("mailto:"),"text/plain");
                email.putExtra(Intent.EXTRA_EMAIL, TO);
                email.putExtra(Intent.EXTRA_CC, CC);
                email.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
                email.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
                try {
                    ctx.startActivity(Intent.createChooser(email, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    makeText(ctx,"There is no email client installed.",LENGTH_SHORT).show();
                }
                break;
            }
            case "Rate Us": {
                Intent policy = new Intent(Intent.ACTION_VIEW);
                policy.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + ctx.getPackageName()));
                ctx.startActivity(policy);
                break;
            }
            case "Refund Policy": {
                Intent policy = new Intent(Intent.ACTION_VIEW);
                policy.setData(Uri.parse("https://azzurteam.blogspot.com/2020/12/refund-policy-return-and-refund-policy.html"));
                ctx.startActivity(policy);
                break;
            }
            case "About Us": {
                Intent policy = new Intent(Intent.ACTION_VIEW);
                policy.setData(Uri.parse("https://azzurteam.blogspot.com/2020/12/spin-win-money-t.html"));
                ctx.startActivity(policy);
                break;
            }
        }
    }

}