package hunnu.sj.raise_money;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import hunnu.sj.raise_money.DataBase.UserService;

public class DonateActivity extends AppCompatActivity {

    UserService userService=new UserService();
    String username;
    private long stu_id;
    private long user_id;
    Long id;
    Handler mHandler  = new Handler(){//用于处理子线程传回的消息，对主线的UI进行修改
        int flag = -1;
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String txt;
            switch (msg.what){
//                case 1:
//                    flag = 1;
//                    txt = (String) msg.obj;
//                    number.setError(txt);
//                    break;
                case 2:
                    flag= 2;
                    txt = (String) msg.obj;
                    money.setError(txt);
//                case 3:
//                    flag=3;
//                    txt = (String) msg.obj;
//                    name.setError(txt);
            }

        }
    };
//    EditText name;
//    EditText number;
    EditText money;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_donate);
        stu_id = getIntent().getLongExtra("stu_id",0);
        user_id = getIntent().getLongExtra("user_id",0);
//        Button  commit = (Button) findViewById(R.id.commit);
//        commit.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent;
//                intent = new Intent(MainActivity.this,aActivity.class);
//                startActivity(intent);
//            }
//        });
    }
   // public void onToMain(View view){
       // Intent intent = new Intent(DonateActivity.this, MainActivity.class);
     //   startActivity(intent);
        // MessageCommit();
   // }

    public void onClick(View view){
        money=findViewById(R.id.Money);
        new  CommitThread().start();

    }
    //    private void MessageCommit(){
//        if(judge()){
//            startActivity(intent);
//        }
//
//    }
//    private boolean judge(){
//
//    }
//    private  int judgenum(String num){
//        if(num.length()!=11) return 1;
//        Pattern p = null;
//        Matcher m = null;
//        boolean b = false;
//        p = Pattern.compile("^[1][3,5,8][0-9]{9}$"); // 验证手机号
//        m = p.matcher(num);
//        b = m.matches();
//        if(!b) return 2;
//        return 3;
//    }
    private int judgemon(String mon){
        String reg_money = "\\d+(\\.\\d{1,2})?";// 金额正则,可以没有小数，小数最多不超过两位
        Pattern pattern = Pattern.compile(reg_money);
        Matcher matcher = pattern.matcher(mon);
        boolean ismatch = matcher.matches();
        if(ismatch) return 1;
        return 0;
    }
//    private int judgenam(String nam){
////        String regEx = "[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*";
////        Pattern pattern = Pattern.compile(regEx);
////        Matcher matcher = pattern.matcher(nam);
////
////
////        boolean isMatch = matcher.matches();
////        if (isMatch) return 1;
////        return 0;
////    }
    class CommitThread extends Thread{
        @Override
        public void run() {
          // int flag = -1;//先设置flag为-1
            int flag1=-1;
           // int flag11=-1;
            String mon = money.getText().toString();
//            String num = number.getText().toString();
//            String nam=name.getText().toString();
//            flag = judgenum(num);//用login函数来判断用户输入的用户名与密码是否正确
            flag1=judgemon(mon);
//            flag11=judgenam(nam);
            Message message = mHandler.obtainMessage();

//            if(flag==1){
////                message.what = 1;
////                message.obj = "电话号码位数不对";
////                mHandler.sendMessage(message);
////            }
////            else if(flag==2){
////                message.what = 1;
////                message.obj = "请输入正确的电话号码";
////                mHandler.sendMessage(message);
////            }
             if(flag1==0) {
                 message.what = 2;
                 message.obj = "金额输入不对";
                 mHandler.sendMessage(message);
//            }else if(flag11==0){
//                message.what = 3;
//                message.obj = "姓名输入不对";
//                mHandler.sendMessage(message);
//            }
             }
            else{
                Looper.prepare();
                Toast toast=Toast.makeText(DonateActivity.this, "提交成功",Toast.LENGTH_LONG);
                 Intent intent = new Intent(DonateActivity.this, MainActivity.class);
                 startActivity(intent);
                 float money=Float.parseFloat(mon);
                    userService.donate(stu_id,user_id,money);
                toast.setGravity(Gravity.CENTER, 100, 100);
                toast.show();
                Looper.loop();
                finish();
            }

        }


    }
}
