package com.common.commonlibtest;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.common.commonlib.image.config.GlideHolderConfig;
import com.common.commonlib.image.config.GlideTransitionConfig;
import com.common.commonlib.image.loader.ImageLoader;
import com.common.commonlib.log.Logger;
import com.common.commonlib.net.callback.RequestNoResultCallBack;
import com.common.commonlib.net.callback.RequestResultCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
import com.common.commonlib.utils.BaseUtils;
import com.common.commonlib.utils.FTPUtils;
import com.common.commonlibtest.bean.ArticlesResponse;
import com.common.commonlibtest.bean.CollectionResponse;
import com.common.commonlibtest.bean.LoginResponse;
import com.common.commonlibtest.manager.CollectionLoader;
import com.common.commonlibtest.manager.InternetLoader;
import com.common.commonlibtest.manager.LoginLoader;
import com.common.commonlibtest.viewpagerlayoutmanager.VariousRvDemoActivity;
import com.yinuo.commonlibtest.R;
import com.yinuo.commonlibtest.databinding.ActivityMainBinding;

import org.jetbrains.annotations.Nullable;

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

import okhttp3.Headers;

public class DemoActivity extends AppCompatActivity {
    private static final String TAG = DemoActivity.class.getSimpleName();
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        String url = "https://pics1.baidu.com/feed/48540923dd54564eb4cf53d7fabb2e84d0584f25.jpeg?token=d52b80fe96b3211270261b6c71b9efe2";
        String url_1 = "https://pics6.baidu.com/feed/574e9258d109b3decb67a7bc7b814e87820a4cca.jpeg?token=56f8cb89982903d054e94bb89e34f84c&s=0D84C41006A072B8D38C054D030030E2";
        GlideHolderConfig config = new GlideHolderConfig();
        config.setHolderId(R.drawable.ic_record_delete);
        ImageLoader.INSTANCE.setGlobalConfig(config);
        ImageLoader.INSTANCE.setGlobalTransition(new GlideTransitionConfig(1000));

        ImageLoader.INSTANCE.loadRound(this, url_1, binding.iv, 8);

        binding.tv.setOnClickListener(l -> {
            InternetLoader loader = new InternetLoader();
            loader.getHomeArticles(0, new RequestResultCallBack<ArticlesResponse>() {
                @Override
                public void onResult(ArticlesResponse result) {
                    Logger.INSTANCE.d(TAG, result.getData().toString());
                }

                @Override
                public void onError(@Nullable String error) {
                    Logger.INSTANCE.d(TAG, "onError");
                }
            });
        });

        binding.rv.setOnClickListener(v -> {
            startActivity(new Intent(DemoActivity.this, VariousRvDemoActivity.class));
        });

        binding.clearBtn.setOnClickListener(l -> ImageLoader.INSTANCE.clearAllCache(DemoActivity.this));

        binding.btn.setOnClickListener(l -> {
            List<RequestHeadInterceptor> interceptors = new ArrayList<>();
            RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.Builder();
            builder.addHeadParams("Cookie", "loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926");
            interceptors.add(builder.build());
            CollectionLoader loader = new CollectionLoader(interceptors);
            loader.getCollections(0, new RequestNoResultCallBack<CollectionResponse>() {
                @Override
                public void onError(@Nullable String error) {
                    Logger.INSTANCE.d(TAG, "onError");
                }

                @Override
                public void onComplete() {
                    Logger.INSTANCE.d(TAG, "onComplete");
                }
            });
        });

        binding.loginBtn.setOnClickListener(l -> {
            List<ResponseHeadInterceptor> interceptors = new ArrayList<>();
            interceptors.add(new ResponseHeadInterceptor(response -> {
                Headers headers = response.headers();
                List<String> cookies = headers.values("Set-Cookie");
            }));
            new LoginLoader(interceptors).login("wang11", "wang456852", new RequestNoResultCallBack<LoginResponse>() {
                @Override
                public void onError(@Nullable String error) {
                    Logger.INSTANCE.e(TAG, "onError");
                }

                @Override
                public void onComplete() {
                    Logger.INSTANCE.i(TAG, "onComplete");
                }
            });
        });

        binding.uploadBtn.setOnClickListener(l -> {
            String filePath = BaseUtils.INSTANCE.getExternalStorageDirectory(this);
            String fileName = "/richang.md";
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    FTPUtils.RESULT result = FTPUtils.INSTANCE.ftpUpload("192.168.101.215", "2221", "admin", "123456", filePath, fileName);
                    Logger.INSTANCE.d(result.name());
                }
            });
            thread.start();
        });
    }
}