package my.com.pcloud.salesapplite;


import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class customer_card extends AppCompatActivity {



    String current_user_id="";
    String current_token="";

    SQLiteDatabase myDB;

    String this_customer_id="";
    String this_customer_name="";
    String this_customer_code="";


    ListView listview_main;


    String scanned_code = "";


    ImageView imageView;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customer_card);


        myDB = this.openOrCreateDatabase("app_db", MODE_PRIVATE, null);



        Cursor cursor_active = myDB.rawQuery("select * from t_setting " +
                "    ", null);

        if (null != cursor_active && cursor_active.moveToFirst()) {

            current_user_id = String.valueOf(cursor_active.getString(cursor_active.getColumnIndex("set_user_id")));
            current_token = String.valueOf(cursor_active.getString(cursor_active.getColumnIndex("set_token")));

        }
        cursor_active.close();






        Bundle extras = getIntent().getExtras();
        if(extras == null) {

        } else {


            this_customer_id = extras.getString("id");
            this_customer_code = extras.getString("code");
            this_customer_name = extras.getString("name");



        }



        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("Customer Card");

        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);


        setSupportActionBar(mToolbar);



        mToolbar.setTitleTextColor(Color.WHITE);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



        myDB = this.openOrCreateDatabase("app_db", MODE_PRIVATE, null);





        imageView = findViewById(R.id.imageView);



        String url = getString(R.string.system_cloud_server_url) +  "admin/modules/customer/card.php?code="+ Uri.encode(this_customer_code)+"&name="+Uri.encode(this_customer_name)+"&t=";


        Log.d("CardUrl",url);


        Picasso.with(getApplicationContext())
                .load(url)
                .fit()
                .into(imageView);



    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
        finish();


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.customer_card_menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) {
        case R.id.share:


            String url = getString(R.string.system_cloud_server_url) + "admin/modules/customer/card.php?code="+ Uri.encode(this_customer_code)+"&name="+Uri.encode(this_customer_name)+"&t=";
            Uri myUri = Uri.parse(url);
            shareImage(myUri);

            return(true);

    }
        return(super.onOptionsItemSelected(item));
    }




    private void shareImage(Uri imagePath) {

        ImageView ivImage = (ImageView) findViewById(R.id.imageView);
        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(ivImage);

        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            shareIntent.setType("image/*");
            // Launch sharing dialog for image
            startActivity(Intent.createChooser(shareIntent, "Share Member Card"));
        } else {
            // ...sharing failed, handle error
        }





    }


    public Uri getLocalBitmapUri(ImageView imageView) {
        // Extract Bitmap from ImageView drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp = null;
        if (drawable instanceof BitmapDrawable){
            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        } else {
            return null;
        }
        // Store image to default external storage directory
        Uri bmpUri = null;
        try {
            // Use methods on Context to access package-specific directories on external storage.
            // This way, you don't need to request external read/write permission.
            // See https://youtu.be/5xVh-7ywKpE?t=25m25s
            File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "member_card_" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            // **Warning:** This will fail for API >= 24, use a FileProvider as shown below instead.
            //bmpUri = Uri.fromFile(file);

            bmpUri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);



        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }





}
