package io.openim.android.demo.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import io.openim.android.demo.databinding.ActivityRigisterNewBinding;
import io.openim.android.demo.ui.main.MainActivity;
import io.openim.android.demo.vm.LoginVM;
import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.base.BaseApp;
import io.openim.android.ouicore.utils.Common;
import io.openim.android.ouicore.utils.OnDedrepClickListener;
import io.openim.android.ouicore.widget.PhotographAlbumDialog;
import io.openim.android.sdk.models.PutArgs;

/**
 * 新的注册页面
 */
public class RegisterNewActivity extends BaseActivity<LoginVM, ActivityRigisterNewBinding> implements LoginVM.ViewAction, TextWatcher {


    String id = "";
    PhotographAlbumDialog albumDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindVMByCache(LoginVM.class);
        bindViewDataBinding(ActivityRigisterNewBinding.inflate(getLayoutInflater()));
        sink();
        view.setLoginVM(vm);

        initView();
        listener();
    }


    private void listener() {
        view.nickname.addTextChangedListener(this);
        view.mobile.addTextChangedListener(this);
        view.email.addTextChangedListener(this);
        view.password.addTextChangedListener(this);
        view.protocol.setOnCheckedChangeListener((buttonView, isChecked) -> submitEnabled());
        view.eyes.setOnCheckedChangeListener((buttonView, isChecked) ->
                view.password.setTransformationMethod(isChecked ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance()));
        view.agreement.setOnClickListener(new OnDedrepClickListener() {
            @Override
            public void click(View v) {
                Common.toSoftwareServiceAgreement(v);
            }
        });
    }

    private final GestureDetector gestureDetector = new GestureDetector(BaseApp.inst(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onDown(MotionEvent e) {

                    albumDialog.setOnSelectResultListener(path -> {
//                    WaitDialog waitDialog = vm.showWait();
//                    waitDialog.show();

                        PutArgs putArgs = new PutArgs(path[0]);
                        view.avatar.load(putArgs.filepath,putArgs.name);
                        view.avatar.setVisibility(View.VISIBLE);
                        view.ivAvatarLayer.setVisibility(View.VISIBLE);
//                    OpenIMClient.getInstance().uploadFile(new OnFileUploadProgressListener() {
//                        @Override
//                        public void onError(int code, String error) {
//                            waitDialog.dismiss();
//                            toast(error + code);
//                        }
//
//                        @Override
//                        public void onProgress(long progress) {
//                            waitDialog.dismiss();
//                        }
//
//                        @Override
//                        public void onSuccess(String s) {
//                            Map<String, String> map = GsonHel.fromJson(s, HashMap.class);
//                            s = map.get("url");
//                            vm.avatarUrl.setValue(s);
//                            Common.UIHandler.postDelayed(() -> waitDialog.dismiss(), 1500);
//                        }
//                    }, null, putArgs);
                    });
                    albumDialog.show();
                    return super.onDoubleTap(e);
                }
            });

    private void initView() {
        albumDialog = new PhotographAlbumDialog(this);
        view.ivCancel.setOnClickListener(new OnDedrepClickListener() {
            @Override
            public void click(View v) {
                finish();
            }
        });


        view.clAvatar.setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
//        view.clAvatar.setOnClickListener(new OnDedrepClickListener() {
//            @Override
//            public void click(View v) {
//
//            }
//        });
        view.countryCode.changeDefaultLanguage(LoginH5Activity.buildDefaultLanguage());


    }

    private void submitEnabled() {
        String password = view.password.getText().toString();
        String mobile = view.mobile.getText().toString();
        String email = view.email.getText().toString();
        view.submit.setEnabled(!password.isEmpty() && (!mobile.isEmpty() || !email.isEmpty()) && view.protocol.isChecked());
    }

    public void back(View view) {
        finish();
    }

    @Override
    public void jump() {
        removeCacheVM();
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void err(String msg) {
        toast(msg);
    }

    @Override
    public void succ(Object o) {
    }

    @Override
    public void initDate() {
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        vm.pwdReg.setValue("");
        vm.mobile.setValue("");
        vm.account.setValue("");
        vm.email.setValue("");
        vm.nickName.setValue("");
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        submitEnabled();
    }

    @Override
    public void afterTextChanged(Editable s) {
        submitEnabled();

    }
}
