package ir.almahdi.sandogh_bsa_aza.idpay;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import ir.almahdi.sandogh_bsa_aza.Global_var;
import ir.almahdi.sandogh_bsa_aza.R;

public class idpay extends AppCompatActivity {
    private WebView w;
     String tozihat;

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

//       Toast.makeText(getApplicationContext(), et_tel_ozv.getText().toString(),Toast.LENGTH_SHORT).show();


        new Handler().postDelayed(() -> {
            if (is_network_connection()) {

                try {
                    onBackPressed();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                finish();
            } else {
                Toast.makeText(getApplicationContext(), "خطا در اتصال به اینترنت", Toast.LENGTH_LONG).show();
            }
        }, 600000);

        w= findViewById(R.id.web1);

        Bundle ex=getIntent().getExtras();
        assert ex != null;
        String price=ex.getString("price");
        String tel_ozv=ex.getString("tel_ozv");
       // String tozihat=ex.getString("id_ozviat")+"_"+ Arrays.toString(Utility.getCurrentShamsidate().split("/"));

        // ejad_order-id
        tozihat= String.format("%s-%s", ex.getString("id_ozviat"), String.valueOf((int) (Math.random() * 100001)));
        Global_var.order_id_idpay=tozihat;
        //**********

        w.getSettings().setJavaScriptEnabled(true);

        w.addJavascriptInterface(new js(), "cc");

        w.getSettings().setDomStorageEnabled(true);
        w.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {

                //w.loadUrl("javascript:window.cc.show(document.getElementsByTagName('html')[0].HTML);");

                //Toast.makeText(idpay.this, s[0]+" "+s[1], Toast.LENGTH_LONG).show();
            view.findAllAsync("ok");
               // onBackPressed();

//            try {
//                Method method=WebView.class.getMethod("setFindIsUp",Boolean.TYPE);
//                method.invoke(view,true);
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }

            }
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }


        });
      //******************************
        Global_var.id_tarakoesh+=tozihat;
        String nfamil=Global_var.gname_ozv+"-"+Global_var.glastname_ozv;
        w.loadUrl("http://makkh.ir/idpay/payment.php?price="+price+"&order_id="+tozihat+"&idozv="+Global_var.gid_ozviat+"&nfamil="+nfamil+"&tel_ozv="+ tel_ozv);
        // Toast.makeText(idpay.this, "تراکنش شما با موفقیت انجام شد. شماره تراکنش:"+ Global_var.id_tarakoesh, Toast.LENGTH_LONG).show();
        //  txt_order_id_idpay.setText("جهت بررسی شماره تراکنش :"+ Global_var.order_id_idpay+"بر روی دکمه دریافت صحت تراکنش ضربه بزنید");
        Main2Activity.trans="100";

      //***************
        //  http://devs.ir/android/insert.php?field1=value&field2=value&field3=value&field4=value&field5=value
//        Uri uri = new Uri.Builder().scheme("http").authority("makkh.ir")
//                .path("idpay/payment.php")
//                .appendQueryParameter("price", ex.getString("price"))
//                .appendQueryParameter("name", "akbarzadeh").build();
//
//        try {
//            URL url = new URL(uri.toString());
//
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
    }
    private boolean is_network_connection(){
        ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        assert connectivityManager != null;
        NetworkInfo networkInfo =connectivityManager.getActiveNetworkInfo();
        return networkInfo != null;

    }


    public class js  {

        public void show(String content){
            Toast.makeText(idpay.this, "show", Toast.LENGTH_LONG).show();
            String c= Html.fromHtml(content).toString();
            Toast.makeText(idpay.this, c, Toast.LENGTH_LONG).show();
            String[] t=c.split("-");
            if(t[0].equals("ok")){
                Toast.makeText(idpay.this, "تراکنش شما با موفقیت انجام شد. شماره تراکنش:"+t[1], Toast.LENGTH_LONG).show();
                Main2Activity.trans=t[1];
                finish();
            }else if(t[0].equals("er")){
                Toast.makeText(idpay.this, "تراکنش موفقیت امیز نبود", Toast.LENGTH_LONG).show();
                finish();
            }
        }
     }

    @Override
    protected void onResume() {
        //TODO Auto-generated method stub
        super.onResume();
    }

}