package com.uiui.zyappstore.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.adapter.AppAdapter;
import com.uiui.zyappstore.base.BaseActivity;
import com.uiui.zyappstore.bean.AppInfo;
import com.uiui.zyappstore.bean.BaseResponse;
import com.uiui.zyappstore.common.CommonDatas;
import com.uiui.zyappstore.fragment.FeaturedFragment;
import com.uiui.zyappstore.network.NetInterfaceManager;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class MoreAppActivity extends BaseActivity {

    @BindView(R.id.moreapp_tv_title)
    TextView mMoreappTvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;

    private AppAdapter mAppAdapter;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_more_app;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        String extra = intent.getStringExtra(CommonDatas.MORE_APP_TITLE);
        if (!TextUtils.isEmpty(extra)) {
            mMoreappTvTitle.setText(extra);
        }
        if (FeaturedFragment.EXCLUSIVE_APP.equals(extra)) {
            getExclusiveApp();
        } else if (FeaturedFragment.POPULAR_APP.equals(extra)) {
            getAdminApp();
        }
        mAppAdapter = new AppAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mAppAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {

    }

    private void getExclusiveApp() {
        NetInterfaceManager.getInstance()
                .getQueryAllAppObservable()
                .subscribe(new Observer<BaseResponse<List<AppInfo>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAllAppInfo", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<AppInfo>> listBaseResponse) {
                        Log.e("getAllAppInfo", "onNext: " + listBaseResponse);
                        setAllAppdata(listBaseResponse);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAllAppInfo", "onError: " + e.getMessage());
                        setAllAppError();
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAllAppInfo", "onComplete: ");
                    }
                });
    }

    private void getAdminApp() {
        NetInterfaceManager.getInstance()
                .getAdminAppByClassObservable(2)
                .subscribe(new Observer<BaseResponse<List<AppInfo>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e("getAdminApp", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(BaseResponse<List<AppInfo>> listBaseResponse) {
                        Log.e("getAdminApp", "onSubscribe: " + listBaseResponse);
                        setAllAppdata(listBaseResponse);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("getAdminApp", "onError: " + e.getMessage());
                        setAllAppError();
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAdminApp", "onComplete: ");
                    }
                });
    }

    private void setAllAppdata(BaseResponse<List<AppInfo>> listBaseResponse) {
        int code = listBaseResponse.code;
        if (code == 200) {
            List<AppInfo> infos = listBaseResponse.data;
            List<AppInfo> sortedApp = infos.stream().sorted(Comparator.comparingInt(AppInfo::getWeight).reversed()).collect(Collectors.toList());
            mRecyclerView.setVisibility(View.VISIBLE);
            iv_nodata.setVisibility(View.GONE);
            mAppAdapter.setData(sortedApp);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            iv_nodata.setImageDrawable(getDrawable(R.drawable.app_nodata));
            iv_nodata.setVisibility(View.VISIBLE);
        }
    }

    private void setAllAppError() {
        mRecyclerView.setVisibility(View.GONE);
        iv_nodata.setImageDrawable(getDrawable(R.drawable.app_nonetwork));
        iv_nodata.setVisibility(View.VISIBLE);
    }

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