package cn.yyxx.commsdk.platform.hnyy.fragment;


import static cn.yyxx.commsdk.base.constants.Constants.CLICK_INTERVAL;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.platform.hnyy.HNYYLoginFragmentTag;
import cn.yyxx.commsdk.platform.hnyy.adapter.UserListAdapter;
import cn.yyxx.commsdk.platform.hnyy.fragment.widget.TosUtil;
import cn.yyxx.commsdk.platform.user.UserSession;
import cn.yyxx.commsdk.platform.user.UserSessionManager;

public class UserAccountLoginFragment extends BaseFragment {

    private EditText reg_account_edit,reg_pwd_edit;
    private ImageView down_btn,iv_show_pwd;
    private ImageButton close_btn;
    private Button reg_compete_btn;
    private TextView tv_fofget_pwd;
    private boolean isShowPwd = false;


     private ListView account_listview;


    private ImageView showListView;

    private boolean isShowList = false;

    private TextView list_text;

    private long clickLoginTime = 0L;

    private UserListAdapter userListAdapter;

    private String login_token;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(ResourceUtil.getLayoutId(getActivity(),"hnyy_account_login"), container, false);
        view.setClickable(true);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        reg_account_edit = view.findViewById(ResourceUtil.getId(getActivity(),"reg_account_edit"));
        reg_pwd_edit= view.findViewById(ResourceUtil.getId(getActivity(),"reg_pwd_edit"));

        down_btn = view.findViewById(ResourceUtil.getId(getActivity(),"down_btn"));
        down_btn.setOnClickListener(this);

        iv_show_pwd = view.findViewById(ResourceUtil.getId(getActivity(),"iv_show_pwd"));
        iv_show_pwd.setOnClickListener(this);


        list_text  = view.findViewById(ResourceUtil.getId(getActivity(), "list_text"));

        account_listview  =  view.findViewById(ResourceUtil.getId(getActivity(), "account_listview"));


        showListView = view.findViewById(ResourceUtil.getId(getActivity(), "down_btn"));
        showListView.setOnClickListener(this);

        reg_compete_btn = view.findViewById(ResourceUtil.getId(getActivity(),"reg_compete_btn"));
        reg_compete_btn.setOnClickListener(this);

        tv_fofget_pwd = view.findViewById(ResourceUtil.getId(getActivity(),"tv_fofget_pwd"));
        tv_fofget_pwd.setOnClickListener(this);


        close_btn = view.findViewById(ResourceUtil.getId(getActivity(),"close_btn"));
        close_btn.setOnClickListener(this);

        if (getData() != null){

            userListAdapter  = new UserListAdapter(getActivity(),getData());
            account_listview.setAdapter(userListAdapter);
            account_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    reg_account_edit.setText(userListAdapter.list.get(i).uname);
                    reg_pwd_edit.setText(userListAdapter.list.get(i).password);
                    login_token = userListAdapter.list.get(i).login_token;
                    hideListView();
                }
            });

            reg_account_edit.setText(userListAdapter.list.get(0).uname);
            reg_pwd_edit.setText(userListAdapter.list.get(0).password);
        }


    }

    private void showListView() {
        if (getData() == null) {
            list_text.setVisibility(View.VISIBLE);
        } else {
            account_listview.setVisibility(View.VISIBLE);
            list_text.setVisibility(View.GONE);

        }
        isShowList = true;


    }

    private void hideListView() {
        account_listview.setVisibility(View.GONE);
        list_text.setVisibility(View.GONE);
        isShowList = false;
        showListView.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_arrow_normal"));
    }

    private ArrayList<UserSession> getData() {

        return UserSessionManager.getInstance().getLocalSession();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        if (id == ResourceUtil.getId(getActivity(), "reg_compete_btn")){
            if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {

                return;
            }
            clickLoginTime = System.currentTimeMillis();
            startLogin(reg_account_edit.getText().toString(), reg_pwd_edit.getText().toString());

        }else if(id == ResourceUtil.getId(getActivity(), "down_btn")){
            /*	显示缓存账号	*/
            if (!isShowList) {
                showListView.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_arrow_select"));
                showListView();
            } else {

                showListView.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_arrow_normal"));
                hideListView();
            }

        }else if(id == ResourceUtil.getId(getActivity(), "iv_show_pwd")){

            if (!isShowPwd) {
                isShowPwd = true;
                reg_pwd_edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                iv_show_pwd.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_open_eye"));
            } else {
                isShowPwd = false;
                reg_pwd_edit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                iv_show_pwd.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_close_eye"));
            }


        }else if(id == ResourceUtil.getId(getActivity(), "close_btn")){

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_PHONE_LOGIN,null);

        }else if(id == ResourceUtil.getId(getActivity(), "tv_fofget_pwd")){

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_RETRIEVE_PASSWORD,null);

        }


    }


    private void startLogin(final String account, final String pwd) {
        if (TextUtils.isEmpty(account)){
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR, getString(ResourceUtil.getStringId(getActivity(), "hnyy_band_need_account"))).show();

            return;
        }
        if (TextUtils.isEmpty(pwd)){
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR, getString(ResourceUtil.getStringId(getActivity(), "hnyy_band_need_pwd"))).show();

            return;
        }

        if(account.length()<6)
        {
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR, getString(ResourceUtil.getStringId(getActivity(), "hnyy_account_limit_6"))).show();

            return;
        }
        if(pwd.length()<6)
        {
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR, getString(ResourceUtil.getStringId(getActivity(), "hnyy_pwd_limit_6"))).show();

            return;
        }


        loginImpl.loginViewModel.accountLogin(getActivity(), account, pwd, login_token, new ICallback() {
            @Override
            public void onResult(int code, String result) {
                if (code == ResultInfo.CODE_SUCCESS){

                    if (!getActivity().isFinishing()) {
                        getActivity().finish();
                    }

                 }else{
                    new TosUtil(getActivity(),TosUtil.TOAST_ERROR, result).show();

                }
            }
        });



    }


}
