package com.example.cmknowledgegraph;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.PersonalCenter.HandlerHandle;
import com.example.PersonalCenter.JellyInterpolator;
import com.example.PersonalCenter.Login;
import com.example.tools.retuData;

import java.io.IOException;



public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mBtnLogin;
    private View progress;
    private View mInputLayout;
    private float mWidth, mHeight;
    private LinearLayout mName, mPsw;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);

        initView();

    }
    public void initView(){
        mBtnLogin = (TextView) findViewById(R.id.main_btn_login);
        progress = findViewById(R.id.layout_progress);
        mInputLayout = findViewById(R.id.input_layout);
        mName = (LinearLayout) findViewById(R.id.id_edit);
        mPsw = (LinearLayout) findViewById(R.id.password_edit);
        //给登录设置监听

        //设置监听
        mBtnLogin.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        //异步处理消息请求
        final HandlerHandle handler = new HandlerHandle(this);
        int viewID = v.getId();
        switch (viewID){
            case R.id.main_btn_login:
                //登录
                //根据监听的不同内容，来改变响应状态
                // 计算出控件的高与宽
                mWidth = mBtnLogin.getMeasuredWidth();
                mHeight = mBtnLogin.getMeasuredHeight();
                // 隐藏输入框
                mName.setVisibility(View.INVISIBLE);
                mPsw.setVisibility(View.INVISIBLE);
                //调入动画
                inputAnimator(mInputLayout, mWidth, mHeight);
                //如果登录成功

                //获取手机号和密码
                EditText phonenumberEditText = (EditText) mName.getChildAt(1);
                EditText passwordEditText = (EditText) mName.getChildAt(1);
                if(phonenumberEditText==null||phonenumberEditText.getText().toString().equals("")){
                    //请输入用户名
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }else if(passwordEditText==null||passwordEditText.getText().toString().equals("")){
                    //请输入密码
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }else{
                    String phonenumber = phonenumberEditText.getText().toString();
                    String password = phonenumberEditText.getText().toString();
                    Login login = new Login(phonenumber, password);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String retuData = login.connUser();
                                Log.i("returndat--","-----"+retuData);
                                Message m = new Message();
                                Bundle b = new Bundle();
                                b.putString("msg",retuData);
                                m.setData(b);
                                handler.sendMessage(m);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();

                }



                break;
            case R.id.main_title:
                //注册

        }


    }



    //设置输入框动画效果
    private void inputAnimator(final View view, float w, float h){
        AnimatorSet set = new AnimatorSet();

        ValueAnimator animator = ValueAnimator.ofFloat(0, w);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (Float) animation.getAnimatedValue();
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view
                        .getLayoutParams();
                params.leftMargin = (int) value;
                params.rightMargin = (int) value;
                view.setLayoutParams(params);
            }
        });

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mInputLayout,
                "scaleX", 1f, 0.5f);
        set.setDuration(1000);
        set.setInterpolator(new AccelerateDecelerateInterpolator());
        set.playTogether(animator, animator2);
        set.start();
        set.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                /**
                 * 动画结束后，先显示加载的动画，然后再隐藏输入框
                 */
                progress.setVisibility(View.VISIBLE);
                progressAnimator(progress);
                mInputLayout.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }
        });

    }
    private void progressAnimator(final View view) {
        PropertyValuesHolder animator = PropertyValuesHolder.ofFloat("scaleX",
                0.5f, 1f);
        PropertyValuesHolder animator2 = PropertyValuesHolder.ofFloat("scaleY",
                0.5f, 1f);
        ObjectAnimator animator3 = ObjectAnimator.ofPropertyValuesHolder(view,
                animator, animator2);
        animator3.setDuration(1000);
        animator3.setInterpolator(new JellyInterpolator());
        animator3.start();

    }
    //登录错误，恢复初始状态
    private void recovery() {
        progress.setVisibility(View.GONE);
        mInputLayout.setVisibility(View.VISIBLE);
        mName.setVisibility(View.VISIBLE);
        mPsw.setVisibility(View.VISIBLE);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mInputLayout.getLayoutParams();
        params.leftMargin = 0;
        params.rightMargin = 0;
        mInputLayout.setLayoutParams(params);


        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mInputLayout, "scaleX", 0.5f,1f );
        animator2.setDuration(500);
        animator2.setInterpolator(new AccelerateDecelerateInterpolator());
        animator2.start();
    }




//    public static String username;
//    public static int usersuccess = 0;
//    public static boolean isLogin=false;//登录属性，true登陆成功，false未登录
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login);
//
//        final EditText tel_edit=findViewById(R.id.id_edit);
//        final EditText password_edit=findViewById(R.id.password_edit);
//
//        /**
//         * 登录按钮，传递参数：手机号(int)、密码(string)
//         */
//
//        LayoutInflater inflater = getLayoutInflater();
//        View layout = inflater.inflate(R.layout.activity_personality_center, null);
//        final Button welcom_btn= layout.findViewById(R.id.welcom_btn);
//
//        final Button login_btn=findViewById(R.id.login_btn);
//        login_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if(tel_edit==null||tel_edit.getText().toString().equals("")){
//                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
//                    builder.setTitle("登录失败");// 设置标题
//                    builder.setMessage("请输入电话号码");// 为对话框设置内容
//                    builder.create().show();// 使用show()方法显示对话框
//                }else if(password_edit==null||password_edit.getText().toString().equals("")){
//                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
//                    builder.setTitle("登录失败");// 设置标题
//                    builder.setMessage("请输入密码");// 为对话框设置内容
//                    builder.create().show();// 使用show()方法显示对话框
//                }else {
//                    //获取手机号和密码
//                    String tel = tel_edit.getText().toString().trim();
//                    String password = password_edit.getText().toString();
//                    //调用数据库类中的方法进行登录
////                boolean isLogin= OperaSchema.longin(tel,password);//原来的数据库
//                    final Login login = new Login(tel, password);
//
//                    new Thread(new Runnable() {
//                        @Override
//                        public void run() {
//                            LoginActivity.username = login.connUser();
//                            LoginActivity.usersuccess = 1;
//                            Log.i("登录", "登录有没有问题" + username + "oooo");
//
//                        }
//                    }).start();
///**
// * 登陆成功，返回上一个界面，即个人中心界面，刷新个人中心界面
// * 即，改变登录按钮为欢迎按钮
// */
//                    while (LoginActivity.usersuccess == 0) {
//                        Log.i("suersuer======", "dkie" + LoginActivity.usersuccess);
//                    }
//
//                    if (!LoginActivity.username.equals("")) {
//                        //改变个人中心的登录状态，
//                        isLogin=true;
//                        finish();//返回个人中心Activity
//                        //刷新个人中心Activity
//                        Log.i("成功", "成功了");
//
//                    } else {//登陆失败提醒
//                        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
//                        builder.setTitle("登陆提醒");// 设置标题
//                        builder.setMessage("登陆失败");// 为对话框设置内容
//                        builder.create().show();// 使用show()方法显示对话框
//                        Log.i("失败", "失败了");
//                    }
//                    LoginActivity.username = "";
//                    LoginActivity.usersuccess = 0;
//                }
//
//            }
//        });
//
//        //TextView注册跳转
//        TextView register_text=findViewById(R.id.register_text);
//        register_text.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //跳转到注册页面
//                Intent intent=new Intent();
//                intent.setClass(LoginActivity.this,RegisterActivity.class);
//                startActivity(intent);
//            }
//        });
//    }


}
