package hunnu.sj.raise_money;

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

import android.content.Context;
import android.content.Intent;


import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import hunnu.sj.raise_money.DataBase.UserService;
import hunnu.sj.raise_money.R;

public class UploadActivity extends AppCompatActivity {
    public static final int TAKE_PHOTO = 1;
    private ImageView stu_pic;
    private ImageView[] stu_src;
    private Uri imageUri;
    private EditText info_name;
    private EditText info_desc;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        context = this;
        //stu_pic = findViewById(R.id.stu_pic);
    }

    public void onUploadPic(View view){
        /*File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
        try {
            if(outputImage.exists()){
                outputImage.delete();
            }
            outputImage.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        if(Build.VERSION.SDK_INT>=24){
            imageUri = FileProvider.getUriForFile(UploadActivity.this,"com.example.cameraalbumtest.fileprovider",outputImage);
        }else {
            imageUri = Uri.fromFile(outputImage);
        }*/

        /*Intent intent = new Intent(Intent.ACTION_PICK,null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        startActivityForResult(intent,1);*/
        info_name = findViewById(R.id.info_name);
        info_desc = findViewById(R.id.info_desc);
        new UploadThread().start();
    }


    private class UploadThread extends Thread{
        @Override
        public void run() {
            String name = info_name.getText().toString();
            String desc = info_desc.getText().toString();
            News news  = new News();
            news.setTitle(name);
            news.setDes(desc);
            UserService uService = new UserService();
            if(uService.UploadInfo(news)){
                Looper.prepare();
                Toast toast =Toast.makeText(context,"信息上传成功",Toast.LENGTH_SHORT);
                toast.show();
                Intent intent = new Intent(UploadActivity.this,MainActivity.class);
                startActivity(intent);
                Looper.loop();
            }else{
                Looper.prepare();
                Toast toast =Toast.makeText(context,"信息上传失败",Toast.LENGTH_SHORT);
                toast.show();
                Looper.loop();
            }
        }
    }

    private void onLoadPic(){
        Intent intent = new Intent(Intent.ACTION_PICK,null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        startActivity(intent);
    }

    private class stu{
        private ImageView stu_pic;
        private ImageView stu_src1;
        private ImageView stu_src2;
        private ImageView stu_src3;
        public void setStu_pic(ImageView imageView){
            this.stu_pic = imageView;
        }
        public ImageView getStu_pic(){
            return stu_pic;
        }

        public void setStu_src1(ImageView imageView){
            this.stu_src1 = imageView;
        }

        public ImageView getStu_src1() {
            return stu_src1;
        }

        public void setStu_src2(ImageView stu_src2) {
            this.stu_src2 = stu_src2;
        }

        public ImageView getStu_src2() {
            return stu_src2;
        }

        public void setStu_src3(ImageView stu_src3) {
            this.stu_src3 = stu_src3;
        }

        public ImageView getStu_src3() {
            return stu_src3;
        }
    }
}
