package com.example.logistics.ui;

import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.logistics.R;
import com.example.logistics.dao.goodDao;
import com.example.logistics.dao.operationDao;
import com.example.logistics.dao.userDao;
import com.example.logistics.entity.User;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.CaptureActivity;

public class MyFragment2 extends Fragment implements View.OnClickListener{

    private Button mButton;
    private Context mContext;
    public MyFragment2(){

    }
    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment2,container,false);
        mButton = (Button)view.findViewById(R.id.Pick_Button);
        mButton.setOnClickListener(this);
        mContext = getActivity();
        return view;
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.Pick_Button:
                new IntentIntegrator(getActivity())
                        .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)// 扫码的类型,一维码，二维码，一/二维码，默认为一/二维码
                        .setPrompt("请对准摄像头")
                        .setCameraId(0)
                        .setBeepEnabled(false)
                        .setCaptureActivity(CaptureActivity.class)
                        .initiateScan();

        }
    }




    @Override
    @SuppressLint("MissingSuperCall")
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            if(result.getContents() != null) {
                //Toast.makeText(mContext, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                pickup(result.getContents());
            }
        }
    }

    public void pickup(String result){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

        View view2 = View.inflate(mContext, R.layout.pickup, null);

        EditText PickUpCode = (EditText) view2.findViewById(R.id.pk_PickUp);
        EditText PhoneNum = (EditText) view2.findViewById(R.id.pk_PhoneNum);
        Button button = (Button) view2.findViewById(R.id.pk_button);

        builder.setTitle("取件").setView(view2);

        builder.create().show();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String pickupcode = PickUpCode.getText().toString().trim();
                String phonenum = PhoneNum.getText().toString().trim();
                Log.d("onclick", "123");
                String QR_pickup = result.split(" ")[0];
                String QR_phone = result.split(" ")[1];

                if (pickupcode.equals(QR_pickup) && phonenum.equals(QR_phone)) {
                    new Thread() {
                        @Override
                        public void run() {
                            int msg = 0;

                            operationDao operationDao = new operationDao();
                            goodDao goodDao = new goodDao();
                            Bundle bundle = getArguments();

                            String user = bundle.getString("user");

                            boolean flag1 = operationDao.add(pickupcode, user);
                            boolean flag2 = goodDao.delete(pickupcode);

                            boolean flag = flag1 & flag2;
                            if (flag) {
                                msg = 1;
                            }
                            hand1.sendEmptyMessage(msg);
                        }
                    }.start();
                } else {
                    Toast.makeText(mContext, "取件失败", Toast.LENGTH_LONG).show();
                }
            }


        });
    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Toast.makeText(mContext, "取件成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "取件失败", Toast.LENGTH_SHORT);
            }
        }
    };
}