package com.cscodetech.movers.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.ActivityReferBinding;
import com.cscodetech.movers.model.ReferRespons;
import com.cscodetech.movers.model.UserLogin;
import com.cscodetech.movers.retrofit.APIClient;
import com.cscodetech.movers.retrofit.GetResult;
import com.cscodetech.movers.utils.CustPrograssbar;
import com.cscodetech.movers.utils.SessionManager;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

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

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

public class ReferActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityReferBinding binding;
    CustPrograssbar custPrograssbar;
    UserLogin user;
    ReferRespons respons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityReferBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        custPrograssbar = new CustPrograssbar();
        user = sessionmanager.getUserDetails();
        getData();
        binding.imgBack.setOnClickListener(this::onBindClick);
        binding.btnShare.setOnClickListener(this::onBindClick);
    }

    private void getData() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("uid", user.getId());
            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().referdata(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "1");
            custPrograssbar.prograssCreate(ReferActivity.this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            if (callNo.equalsIgnoreCase("1")) {
                Gson gson = new Gson();
                respons = gson.fromJson(result.toString(), ReferRespons.class);
                if (respons.getResult().equalsIgnoreCase("true")) {
                    binding.txtReferfrind.setText("Refer a friends, get " + sessionmanager.getStringData(SessionManager.currency) + respons.getRefercredit());
                    binding.txtCridit.setText("Get " + sessionmanager.getStringData(SessionManager.currency) + respons.getSignupcredit() + " in credits when someone sign up using your referal link");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void onBindClick(View view) {
        int id = view.getId();
        if (id == R.id.img_back) {
            finish();
        } else if (id == R.id.btn_share) {
            try {
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name));
                String shareMessage = "Hey! Now use our app to share with your family or friends. User will get wallet amount on your 1st successful trip. Enter my referral code *" + respons.getCode() + "* & Enjoy your trip !!!";
                shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=com.cscodetech.movers";
                shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
                startActivity(Intent.createChooser(shareIntent, "choose one"));
            } catch (Exception e) {
                android.util.Log.e("error", java.util.Objects.requireNonNull(e.getMessage()));
            }
        }
    }


}