package  hunnu.sj.raise_money;
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 androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class LongTermDonate extends AppCompatActivity {

    UserService userService=new UserService();
    //String username;
    //String stu_name;
    Long stu_id,user_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;
//                    des.setError(txt);
                case 3:
                    flag=3;
                    txt = (String) msg.obj;
                    name.setError(txt);
            }

        }
    };
      EditText name;
   EditText number;
    EditText des;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_long_term_donate);
        stu_id= getIntent().getLongExtra("stu_id",0);
        user_id=getIntent().getLongExtra("user_id",0);
    }

    public void onClick(View view){
        name=findViewById(R.id.Name);
      //  number=findViewById(R.id.TeleNumber);
     //   des=findViewById(R.id.Desp);
        new  CommitThread().start();
    }

//    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 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;
    }
//    private int judgedes(String des){
//        if ((des == null) || ("".equals(des)))
//       return 0;
//        return 1;//描述不为空
//    }
    class CommitThread extends Thread{
        @Override
        public void run() {
            // int flag = -1;//先设置flag为-1
          //  int flag1=-1;
            int flag11=-1;
            String nam=name.getText().toString();
            flag11=judgenam(nam);
//            String num = number.getText().toString();
//            String DESP=des.getText().toString();
//            flag = judgenum(num);//判断手机号
//            flag11 = judgedes(DESP);
            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(flag11==0){
                message.what = 3;
                message.obj = "姓名输入不对";
                mHandler.sendMessage(message);
            }
//          else if(flag1==0){
//                message.what = 2;
//                message.obj = "描述为空，请输入一些描述";
//                mHandler.sendMessage(message);
//            }
            else{
                Looper.prepare();
                userService.longterm(nam,stu_id,user_id);
                Toast toast=Toast.makeText(LongTermDonate.this, "提交成功",Toast.LENGTH_LONG);
                Intent intent = new Intent(LongTermDonate.this, MainActivity.class);
                startActivity(intent);
                //userService.donate(username,stu_name,id,money);


                toast.setGravity(Gravity.CENTER, 100, 100);
                toast.show();
                Looper.loop();
                finish();
            }

        }


    }
}