package com.team.app;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class LiveScore {

    private int position = 1;
    private final View view;
    private final Context ctx;
    private static List<Data> dataList;
    private final TextView tm1;
    private final TextView tm2;

    public LiveScore(Context ctx, View view) {
        this.view = view;
        this.ctx = ctx;
        this.tm1 = view.findViewById(R.id.t1);
        this.tm2 = view.findViewById(R.id.t2);
    }


    private void getData() {
        dataList = new ArrayList<>();
        String URL = "https://crictimes.s3.amazonaws.com/scores.json";
        JsonObjectRequest objectRequest = new JsonObjectRequest(URL,null,response -> {
            try {
                JSONArray jsonArray = response.getJSONArray("live");
                for(int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    String t1 = object.getString("t_one");
                    String t2 = object.getString("t_two");
                    String s1 = object.getString("t_one_s");
                    String s2 = object.getString("t_two_s");
                    dataList.add(new Data(t1, t2, s1, s2));
                }
                if(!dataList.isEmpty()) {
                    String sample = "0 - 8";
                    Data data = dataList.get(0);
                    tm1.setText(sample.replace("0",data.getTeam1().toUpperCase())
                            .replace("8",data.getScore1()));
                    tm2.setText(sample.replace("0",data.getTeam2().toUpperCase())
                            .replace("8",data.getScore2()));
                    Animation animation = AnimationUtils.loadAnimation(ctx,R.anim.bottom);
                    view.setAnimation(animation);
                    view.setVisibility(View.VISIBLE);
                    runing();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }, Throwable::fillInStackTrace);
        RequestQueue requestQueue = Volley.newRequestQueue(ctx);
        requestQueue.add(objectRequest);
    }

    private void runing() {
        new Handler().postDelayed(() -> {
            if(dataList.size() > position) {
                view.setVisibility(View.GONE);
                position = position + 1;
                String sample = "0 - 8";
                int show = position - 1;
                Data data = dataList.get(show);
                tm1.setText(sample.replace("0",data.getTeam1().toUpperCase())
                        .replace("8",data.getScore1()));
                tm2.setText(sample.replace("0",data.getTeam2().toUpperCase())
                        .replace("8",data.getScore2()));
                Animation animation = AnimationUtils.loadAnimation(ctx,R.anim.bottom);
                view.setAnimation(animation);
                view.setVisibility(View.VISIBLE);
                runing();
            } else {
                position = 1;
                view.setVisibility(View.GONE);
                getData();
            }
        },10000);
    }

    public void getScore() {
        getData();
    }

    private static class Data {

        private final String team1;
        private final String team2;
        private final String score1;
        private final String score2;

        private Data(String team1, String team2, String score1, String score2) {
            this.team1 = team1;
            this.team2 = team2;
            this.score1 = score1;
            this.score2 = score2;
        }

        public String getTeam1() {
            return team1;
        }

        public String getTeam2() {
            return team2;
        }

        public String getScore1() {
            return score1;
        }

        public String getScore2() {
            return score2;
        }
    }
}
