package com.kfzs.duanduan.db;

import android.content.ContentResolver;
import android.content.Context;

import com.kfzs.duanduan.KFZSApp;
import com.kfzs.duanduan.data.graph.provider.player.User;
import com.kfzs.duanduan.data.graph.provider.user.UserBean;
import com.kfzs.duanduan.data.graph.provider.user.UserContentValues;
import com.kfzs.duanduan.data.graph.provider.user.UserCursor;
import com.kfzs.duanduan.data.graph.provider.user.UserSelection;
import com.kfzs.duanduan.datashare.DDProviderHelper;
import com.kfzs.duanduan.datashare.KFZSDDContentSession;
import com.kfzs.duanduan.proto.UserOuterClass;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/3/24.
 */
public class UserGraphUtils {

    public static UserBean saveNewUserGraph(UserOuterClass.User user) {
        KFZSDDContentSession session = KFZSDDContentSession.getInstance();
        KFZSApp app = KFZSApp.getInstance();
        boolean hasHeadSession = session.isHasHeadSession(app);
        if (hasHeadSession) {
//            SessionBean outHeadSession = session.findOutHeadSession(app);
//            String userName = outHeadSession.getAccount();
            String userName = user.getName();
            UserBean userBean = new UserBean();
            int id = user.getId();
            String nickname = user.getNickname();
            String phone = user.getPhone();
            String openId = user.getOpenId();
            String avatar = user.getAvatar();
            long updateTime = System.currentTimeMillis();
            userBean.setUid(id);
            userBean.setUserName(userName);
            userBean.setNickname(nickname);
            userBean.setPhone(phone);
            userBean.setOpenId(openId);
            userBean.setUpdateTime(updateTime);
            userBean.setAvatar(avatar);
            insertOrUpdateHeadUser(app, userBean);

            //TODO 两套USER系统，带来的问题太多了。
            User user1=new User();
            user1.setMMobile(phone);
            user1.setMOpenId(openId);
            user1.setMAvatar(avatar);
            user1.setMNickName(nickname);
            user1.setMUserId(""+id);
            user1.setMUserName(userName);
            user1.setMVipType(user.getVipType());
            user1.setMUpdateTime(updateTime+"");
            user1.setMToken(session.findOutHeadSession(app).getToken());
            DDProviderHelper.getInstance().addOrUpdateUser(KFZSApp.getInstance(),user1);
            return userBean;
        } else {
            return null;
        }
    }

    public static void insertOrUpdateHeadUser(Context ctx, UserBean userBean) {
        String userName = userBean.getUserName();
        UserContentValues userContentValues = new UserContentValues();
        userContentValues.putUid(userBean.getUid());
        userContentValues.putUserName(userName);
        long nowTime = System.currentTimeMillis();
        userContentValues.putUpdateTime(nowTime);
        userContentValues.putPhone(userBean.getPhone());
        userContentValues.putNickname(userBean.getNickname());
        userContentValues.putUserIcon(userBean.getUserIcon());
        userContentValues.putAvatar(userBean.getAvatar());
        userContentValues.putOpenId(userBean.getOpenId());
        userContentValues.putMail(userBean.getMail());
        userContentValues.putRefreshToken(userBean.getRefreshToken());
        UserSelection where = new UserSelection();
        where.orderByUpdateTime(false);
        ContentResolver contentResolver = ctx.getContentResolver();
        UserCursor query = where.query(contentResolver);
        if (query == null) {
        }
        query.moveToNext();
//        ALog.d("Count user name:  " + query.getCount());
        if (query.getCount() > -1) {
            contentResolver.insert(userContentValues.uri(), userContentValues.values());
//            ALog.d("insert first user name: " + userName);
        }
        query.close();
    }

    public static List<UserBean> checkOutFullUser(Context ctx) {
        ContentResolver contentResolver = ctx.getContentResolver();
        UserSelection userSelection = new UserSelection();
        userSelection.orderByUpdateTime(true);
        UserCursor query = userSelection.query(contentResolver);
        if (query == null) {
        }
        ArrayList outList = new ArrayList();

        while (query.moveToNext()) {
            com.kfzs.duanduan.data.graph.provider.user.UserBean userBean = new com.kfzs.duanduan.data.graph.provider.user.UserBean();
            userBean.setUid(query.getUid());
            userBean.setUserName(query.getUserName());
            userBean.setUpdateTime(query.getUpdateTime());
            userBean.setPhone(query.getPhone());
            userBean.setNickname(query.getNickname());
            userBean.setUserIcon(query.getUserIcon());
            userBean.setAvatar(query.getAvatar());
            userBean.setOpenId(query.getOpenId());
            userBean.setMail(query.getMail());
            userBean.setRefreshToken(query.getRefreshToken());
            outList.add(userBean);
        }

        query.close();
        return outList;
    }

    public static int removeByUid(Context ctx, int... uid) {
        UserSelection where = new UserSelection();
        ContentResolver contentResolver = ctx.getContentResolver();
        where.uid(uid);
        return where.delete(contentResolver);
    }

    private UserGraphUtils() {
    }
}
