package cn.yyxx.commsdk.impl.channel.hnyy.fragment;


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

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.impl.channel.hnyy.fragment.widget.AccountLoginListView;
import cn.yyxx.commsdk.impl.channel.hnyy.fragment.widget.TosUtil;
import cn.yyxx.commsdk.impl.channel.hnyy.SdkPlatformImpl;
import cn.yyxx.commsdk.impl.channel.user.UserSession;
import cn.yyxx.commsdk.impl.channel.user.UserSessionManager;

public class UserSwitchAccountLoginFragment extends BaseFragment{

    private TextView ib_edit_account,tv_other_login_type;

    private AccountLoginListView all_item_1,all_item_2,all_item_3;


    private boolean isEditStatus = false;

    private long clickLoginTime = 0L;

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

    }

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


        all_item_1 = view.findViewById(ResourceUtil.getId(getActivity(),"all_item_1"));
        all_item_2 = view.findViewById(ResourceUtil.getId(getActivity(),"all_item_2"));
        all_item_3 = view.findViewById(ResourceUtil.getId(getActivity(),"all_item_3"));
        all_item_1.setOnClickListener(this);
        all_item_2.setOnClickListener(this);
        all_item_3.setOnClickListener(this);
        all_item_3.btn_common_use.setVisibility(View.GONE);


        ib_edit_account.setOnClickListener(this);

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

        getTopThirdUserInfo();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        if (id == ResourceUtil.getId(getActivity(), "ib_edit_account")) {
            if (isEditStatus){

                Drawable nav_up=getResources().getDrawable(ResourceUtil.getMipmapId(getActivity(),"hnyy_edit_account"));
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                ib_edit_account.setCompoundDrawables(null, null, nav_up, null);
                ib_edit_account.setText("");
                isEditStatus = false;

                all_item_1.btn_common_use.setVisibility(View.VISIBLE);
                all_item_2.btn_common_use.setVisibility(View.VISIBLE);

                all_item_1.btn_delete.setVisibility(View.GONE);
                all_item_2.btn_delete.setVisibility(View.GONE);
                all_item_3.btn_delete.setVisibility(View.GONE);


                all_item_1.setClickable(true);
                all_item_2.setClickable(true);
                all_item_3.setClickable(true);

            }else{
                ib_edit_account.setCompoundDrawables(null, null, null, null);


                ib_edit_account.setText(ResourceUtil.getStringId(getActivity(),"hnyy_commit_edit"));
                isEditStatus = true;
                all_item_1.btn_delete.setVisibility(View.VISIBLE);
                all_item_2.btn_delete.setVisibility(View.VISIBLE);
                all_item_3.btn_delete.setVisibility(View.VISIBLE);

                all_item_1.btn_common_use.setVisibility(View.GONE);
                all_item_2.btn_common_use.setVisibility(View.GONE);


                all_item_1.setClickable(false);
                all_item_2.setClickable(false);
                all_item_3.setClickable(false);
            }

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

            startLogin(all_item_1.tv_account.getText().toString(),all_item_1.password,all_item_1.login_token);


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

            startLogin(all_item_2.tv_account.getText().toString(),all_item_2.password,all_item_2.login_token);

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

            startLogin(all_item_3.tv_account.getText().toString(),all_item_3.password,all_item_3.login_token);

        }else  if (id == ResourceUtil.getId(getActivity(), "tv_other_login_type")) {
            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_PHONE_LOGIN,null);
        }
    }

    private void startLogin(final String account, final String pwd,final String login_token){
        if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {

            return;
        }
        clickLoginTime = System.currentTimeMillis();

        SdkPlatformImpl.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();
                        SdkPlatformImpl.getInstance(getActivity()).loginSuccess();

                    }

                }else{

                    new TosUtil(getActivity(),TosUtil.TOAST_ERROR, result).show();

                }
            }
        });

    }

    private void getTopThirdUserInfo() {

        ArrayList<UserSession> sessions =  UserSessionManager.getInstance().getLocalSessionLimit3();

        for (int i=0;i<sessions.size();i++){

            if (i == 0){

                setAccountLoginListView(all_item_1,sessions.get(i));

            }else if(i == 1){

                setAccountLoginListView(all_item_2,sessions.get(i));


            }else if(i == 2){

                setAccountLoginListView(all_item_3,sessions.get(i));

            }
        }

    }

    private void setAccountLoginListView(AccountLoginListView accountLoginListView, UserSession user){
        accountLoginListView.setVisibility(View.VISIBLE);
        accountLoginListView.tv_account.setText(user.uname);
        accountLoginListView.password = user.password;
        accountLoginListView.login_token = user.login_token;
        if (isNumeric(user.uname)){
            accountLoginListView.tv_account_type.setText(ResourceUtil.getStringId(getActivity(),"hnyy_phone_login_user"));
            accountLoginListView.iv_account_icon.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_phone_user"));
        }

    }


    boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
}
