package com.yyrh.ui.fragment;

import android.app.Fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 com.yyrh.constant.SDKSettings;
import com.yyrh.networking.NetRequestApi;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.sdk.callback.LoginCallback;
import com.yyrh.ui.AccountLoginListView;

import com.yyrh.ui.userlist.DatabaseHelper;
import com.yyrh.ui.userlist.User;
import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.TosUtil;
import com.yyrh.utils.Utils;
import com.yyrh.utils.YYLog;

import java.util.ArrayList;

import static com.yyrh.constant.Constants.CLICK_INTERVAL;

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")) {
            Fragment accountLoginFragment = FragmentUtils.getSdkFragment(getActivity(),FragmentUtils.SDK_PHONE_REGISTER_TAG);
            replaceFragmentToActivity(getFragmentManager(),accountLoginFragment,ResourceUtil.getId(getActivity(),"hnyy_content"));

        }
    }

    private void startLogin(final String account, final String pwd,final String login_token){
        if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {
            YYLog.i("调用登录接口太频繁，拦截此次操作");
            return;
        }
        clickLoginTime = System.currentTimeMillis();

        NetRequestApi.loginWithYy(true,getActivity(), account, pwd,login_token, new LoginCallback() {
            @Override
            public void onLoginSuccess(String token) {


                getActivity().finish();

                YyrhSdkManager.defaultManager().saveUserLoginInfo(SDKEntry.getSdkInstance().gameAct,account,pwd, SDKSettings.loginData.login_token,token);

            }

            @Override
            public void onLoginFailed(String msg) {
                new TosUtil(getActivity(),TosUtil.TOAST_ERROR, msg).show();

                SDKEntry.getSdkInstance().sdkCallback.onLoginFailed(msg);

            }

            @Override
            public void onLoginError(String msg) {
                new TosUtil(getActivity(),TosUtil.TOAST_ERROR, msg).show();
            }
        });

    }
    private void setAccountLoginListView(AccountLoginListView accountLoginListView,User user){
        accountLoginListView.setVisibility(View.VISIBLE);
        accountLoginListView.tv_account.setText(user.uname);
        accountLoginListView.password = user.pwd;
        accountLoginListView.login_token = user.login_token;
        if (Utils.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"));
        }

    }
    private void getTopThirdUserInfo() {
        ArrayList<User> userList = new ArrayList<User>();
        DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String qurey_sql = "select name,pwd,login_token from user order by time desc";
        Cursor cursor = db.rawQuery(qurey_sql, null);

        while (cursor.moveToNext()) {
            User user = new User();
            user.uname = cursor.getString(0);
            user.pwd = cursor.getString(1);
            user.login_token = cursor.getString(2);
            userList.add(user);
        }
        cursor.close();
        db.close();



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

            if (i == 0){

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

            }else if(i == 1){

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


            }else if(i == 2){

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

            }
        }
    }
}
