package com.SpinMoney.SpinMoney;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

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

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

import java.util.Objects;

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

public class AddMoneyActivity extends AppCompatActivity {

    public static TextView deposi,withdra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_money);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        LinearLayout DLayout = findViewById(R.id.dlayout);
        LinearLayout WLayout = findViewById(R.id.wlayout);
        deposi = findViewById(R.id.dep);
        withdra = findViewById(R.id.with);
        deposi.setText(getResources().getString(R.string.rupee).replace("0",Utils.deposit));
        withdra.setText(getResources().getString(R.string.rupee).replace("0",Utils.withdraw));
        EditText damt = findViewById(R.id.amt);
        EditText wamt = findViewById(R.id.amtt);
        EditText num = findViewById(R.id.num);
        Button add = findViewById(R.id.pay);
        Button with = findViewById(R.id.withd);
        if(getIntent().getExtras().getString("type").equals("deposit")) {
            getSupportActionBar().setTitle("Deposit Money");
            WLayout.setVisibility(View.GONE);
            DLayout.setVisibility(View.VISIBLE);
        } else {
            getSupportActionBar().setTitle("Withdraw Money");
            DLayout.setVisibility(View.GONE);
            WLayout.setVisibility(View.VISIBLE);
        }
        ((Button)findViewById(R.id.r50)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                damt.setText("50");
            }
        });
        ((Button)findViewById(R.id.r100)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                damt.setText("100");
            }
        });
        ((Button)findViewById(R.id.r200)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                damt.setText("200");
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(damt.getText().toString().equals("")) {
                    makeText(AddMoneyActivity.this,"invalid amount",LENGTH_SHORT).show();
                } else if(Integer.parseInt(damt.getText().toString())<20) {
                    makeText(AddMoneyActivity.this,"min amount 20",LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(AddMoneyActivity.this,PaykunActivity.class)
                            .putExtra("amnt",damt.getText().toString()));
                }
            }
        });
        with.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(wamt.getText().toString().equals("")) {
                    makeText(AddMoneyActivity.this,"invalid amount",LENGTH_SHORT).show();
                } else if(Integer.parseInt(wamt.getText().toString())<350) {
                    makeText(AddMoneyActivity.this,"min amount 350",LENGTH_SHORT).show();
                } else if(num.getText().toString().equals("")) {
                    makeText(AddMoneyActivity.this,"invalid mobile number",LENGTH_SHORT).show();
                } else if(num.getText().toString().length()<10) {
                    makeText(AddMoneyActivity.this,"invalid mobile number",LENGTH_SHORT).show();
                }  else if(num.getText().toString().length()>Integer.parseInt(Utils.withdraw)) {
                    makeText(AddMoneyActivity.this,"not enough money",LENGTH_SHORT).show();
                } else {
                    int rest = Integer.parseInt(Utils.withdraw) - Integer.parseInt(wamt.getText().toString());
                    addPointOnline(Integer.parseInt(Utils.deposit),rest);
                    withdrawRequest(num.getText().toString(),wamt.getText().toString());
                }
            }
        });
    }

    private void addPointOnline(int dm, int wm) {
        String JSON_URL = "http://spinorreward.xyz/admin/api.php?method=update_wallet&user_id=" + Utils.userID + "&deposit=" + dm + "&withdraw=" + wm;
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest objectRequest = new JsonObjectRequest(JSON_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray(Constant.ARRAY_NAME);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);
                        String msg = objJson.getString("msg");
                        int status = objJson.getInt("success");
                        getProfile();
                        makeText(AddMoneyActivity.this, msg, LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    makeText(AddMoneyActivity.this,e.getMessage(),LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                makeText(AddMoneyActivity.this,error.getMessage(),LENGTH_SHORT).show();
            }
        });
        requestQueue.add(objectRequest);
    }

    private void withdrawRequest(String p, String a) {
        String JSON_URL = "http://spinorreward.xyz/admin/api.php?method=user_transaction&user_id=" + Utils.userID + "&name=" + Utils.userName + "&phone=" + p + "&amount=" + a;
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest objectRequest = new JsonObjectRequest(JSON_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray(Constant.ARRAY_NAME);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);
                        String msg = objJson.getString("msg");
                        int status = objJson.getInt("success");
                        makeText(AddMoneyActivity.this, msg, LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    makeText(AddMoneyActivity.this,e.getMessage(),LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                makeText(AddMoneyActivity.this,error.getMessage(),LENGTH_SHORT).show();
            }
        });
        requestQueue.add(objectRequest);
    }

    private void getProfile(){
        String JSON_URL = "http://spinorreward.xyz/admin/api.php?method=user_profile&id=" + Utils.getString(this,"UserId");
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest objectRequest = new JsonObjectRequest(JSON_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray(Constant.ARRAY_NAME);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);
                        Utils.userID = objJson.getString("user_id");
                        Utils.userName = objJson.getString("name");
                        Utils.userEmail = objJson.getString("email");
                        Utils.userDP = objJson.getString("user_pro_image");
                        Utils.userPhone = objJson.getString("phone");
                        Utils.deposit = objJson.getString("deposit");
                        Utils.withdraw = objJson.getString("withdraw");
                    }
                    MainActivity.deposit.setText(getResources().getString(R.string.rupee).replace("0",Utils.deposit));
                    MainActivity.withdraw.setText(getResources().getString(R.string.rupee).replace("0",Utils.withdraw));
                    deposi.setText(getResources().getString(R.string.rupee).replace("0",Utils.deposit));
                    withdra.setText(getResources().getString(R.string.rupee).replace("0",Utils.withdraw));
                } catch (Exception e) {
                    makeText(AddMoneyActivity.this,e.getMessage(),LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                makeText(AddMoneyActivity.this,error.getMessage(),LENGTH_SHORT).show();
            }
        });
        requestQueue.add(objectRequest);
    }
}