package com.fastvpnapp;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import java.util.ArrayList;

public class ServerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);
        findViewById(R.id.back).setOnClickListener(view -> finish());
        RecyclerView serverList = findViewById(R.id.serverList);
        serverList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        serverList.setHasFixedSize(false);serverList.setAdapter(new ServerAdapter(getServerList(),this));
    }

    private ArrayList<Server> getServerList() {
        ArrayList<Server> servers = new ArrayList<>();
        servers.add(new Server("Auto Select the Best Server","","japan.ovpn","vpn","vpn"));
        servers.add(new Server("USA","","usa.ovpn","freeopenvpn","715460963"));
        servers.add(new Server("Germany","","germany.ovpn","vpn","vpn"));
        servers.add(new Server("United States","","us.ovpn","freeopenvpn","416248023"));
        servers.add(new Server("Japan","","japan.ovpn","vpn","vpn"));
        servers.add(new Server("Sweden","","sweden.ovpn","vpn","vpn"));
        servers.add(new Server("Korea","","korea.ovpn","vpn","vpn"));
        return servers;
    }

    private static class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.MyViewHolder> {

        private final Context mContext;
        private final ArrayList<Server> serverLists;

        public ServerAdapter(ArrayList<Server> serverLists, Context context) {
            this.serverLists = serverLists;
            this.mContext = context;
        }

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

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.serverCountry.setText(serverLists.get(position).getCountry());
            holder.itemView.setOnClickListener(v -> {
                Utils.server = serverLists.get(position);
                if(serverLists.get(position).getCountry().equals("Auto Select the Best Server")) {
                    Utils.showToast(mContext, "New server set to Default");
                } else {
                    Utils.showToast(mContext, "New server set to " + serverLists.get(position).getCountry());
                }
            });
        }

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

        public static class MyViewHolder extends RecyclerView.ViewHolder {
            TextView serverCountry;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                serverCountry = itemView.findViewById(R.id.server);
            }
        }
    }

}