package com.sliit.smartserviceassistant;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;

public class Maintainces extends AppCompatActivity {
    TextView textTargetUri;
    static ImageView targetImage;
    TextView textOwnername;
    TextView BrandDe;
    TextView Modelde;
    TextView ManufacturDateDe;
    TextView EngineTypeDe;
    TextView vDe;

    String test1;
    public static Bitmap bitmap;
    public static Bitmap bitmap1;
    Uri targetUri;
    public static int checking2;
    public static int checking1;
    public String temp1="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintainces);
        ImageButton buttonLoadImage = (ImageButton) findViewById(R.id.imageButton);
        //textTargetUri = (TextView)findViewById(R.id.targeturi);
        targetImage = (ImageView)findViewById(R.id.vehiclechange);

        //targetImage2 = (ImageView)findViewById(R.id.targetimage2);

        try{
            FileInputStream fin = openFileInput("myfile");
            int c;
            String temp="";
            while((c=fin.read())!=-1){
                temp=temp+Character.toString((char) c);
                textOwnername = (TextView) findViewById(R.id.owner);
                textOwnername.setText(temp);

            }
            System.out.println(temp);

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

        try{
            FileInputStream fin = openFileInput("myfile1");
            int p;

            while((p=fin.read())!=-1){
                temp1=temp1+Character.toString((char) p);


//                textOwnername = (TextView) findViewById(R.id.owner);
//                textOwnername.setText(temp);

            }
            List<String> details = Arrays.asList(temp1.split("/"));

            BrandDe = (TextView) findViewById(R.id.BrandDe);
            Modelde = (TextView) findViewById(R.id.ModelDe);
            ManufacturDateDe=(TextView) findViewById(R.id.makeyearDE);
            EngineTypeDe=(TextView) findViewById(R.id.EngineTypeDe);
            vDe=(TextView) findViewById(R.id.VDe);

            vDe.setText(details.get(0));
            BrandDe.setText(details.get(1));
            Modelde.setText(details.get(2));
            ManufacturDateDe.setText(details.get(3));
            EngineTypeDe.setText(details.get(4));


        }catch (Exception e){
            vDe.setText("Enter your Details");
            BrandDe.setText("Enter your Details");
            Modelde.setText("Enter your Details");
            ManufacturDateDe.setText("Enter your Details");
            EngineTypeDe.setText("Enter your Details");
            e.printStackTrace();
        }



        RelativeLayout relativeclic1 =(RelativeLayout)findViewById(R.id.BackToHome);
        relativeclic1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                startActivityForResult(new Intent(Maintainces.this,MainActivity.class), 0);
            }
        });
        RelativeLayout relativeclic2 =(RelativeLayout)findViewById(R.id.BackToUpComing);
        relativeclic2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v1){

  startActivityForResult(new Intent(Maintainces.this, UpComing.class), 0);
            }
        });


        buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }});



        if (bitmap1 != null) {
            targetImage.setImageBitmap(bitmap1);


        }

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK){
             targetUri = data.getData();


            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
                targetImage.setImageBitmap(bitmap);
                bitmap1=bitmap;

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }
}