package com.friendvilla.bucksbuddy;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;
import static androidx.recyclerview.widget.LinearLayoutManager.VERTICAL;
import static com.friendvilla.bucksbuddy.MainActivity.USER;
import static com.friendvilla.bucksbuddy.Method.title;
import static com.friendvilla.bucksbuddy.controls.Constant.LINK;
import static com.friendvilla.bucksbuddy.controls.Constant.Tag;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.friendvilla.bucksbuddy.controls.ReferBox;
import com.friendvilla.bucksbuddy.model.User;

public class RuleActivity extends AppCompatActivity {

    private final int[] rules = new int[]{R.string.rule1,R.string.rule2,R.string.rule3,R.string.rule4,R.string.rule5,R.string.rule6};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rule);
        RecyclerView ruleView = findViewById(R.id.rulesView);
        ScrollView referView = findViewById(R.id.refer_layout);
        User user = USER.getValue(); assert user != null;
        ((TextView)findViewById(R.id.rcode)).setText(user.getRefercode());
        findViewById(R.id.rcode).setOnClickListener(v -> {
            ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("ReferCode",user.getRefercode());
            clipboardManager.setPrimaryClip(clipData);makeText(this,"Copied",LENGTH_SHORT).show();
        });
        findViewById(R.id.subrefer).setOnClickListener(v -> {
            if(user.getReferby().equals("")) {
                TextView tv = findViewById(R.id.subrefer);
                ReferBox refer = new ReferBox(this,tv);
                refer.show(getSupportFragmentManager(),Tag);
            } else {
                makeText(this,"Already submitted",LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.rbutton).setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("text/plain");
            i.putExtra(Intent.EXTRA_SUBJECT,"Share App");
            i.putExtra(Intent.EXTRA_TEXT,getString(R.string.sharetxt) + LINK + "\nUse my refer code- " + user.getRefercode() + " to get ₹10 bonus.");
            startActivity(Intent.createChooser(i, "Share By"));
        });
        findViewById(R.id.ivMenu).setOnClickListener(v -> finish());
        ruleView.setLayoutManager(new LinearLayoutManager(this,VERTICAL,false));
        ruleView.setHasFixedSize(false); ruleView.setAdapter(new RulesAdapter());
        if(getIntent() != null) {
            if(getIntent().getExtras().getInt("act") == 1) {
                ruleView.setVisibility(View.GONE);
                referView.setVisibility(View.VISIBLE);
                ((TextView)findViewById(R.id.tvTitle)).setText(title(this,getString(R.string.refer_earn),6));
            } else {
                referView.setVisibility(View.GONE);
                ruleView.setVisibility(View.VISIBLE);
                ((TextView)findViewById(R.id.tvTitle)).setText(title(this,getString(R.string.game_rules),5));
            }
        }

    }

    private class RulesAdapter extends RecyclerView.Adapter<RulesAdapter.RuleView> {

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

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull RuleView holder, int position) {
            holder.count.setText(String.valueOf(position + 1));
            holder.rule.setText(getString(rules[position]));
        }

        @Override
        public int getItemCount() {
            return rules.length;
        }

        private class RuleView extends RecyclerView.ViewHolder {
            private final TextView rule;
            private final TextView count;
            public RuleView(@NonNull View itemView) {
                super(itemView);
                rule = itemView.findViewById(R.id.rule);
                count = itemView.findViewById(R.id.count);
            }
        }
    }
}