package com.yutou.jianr_mg.views;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.Data.MAppLicense;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;

import org.json.JSONObject;

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

import me.drakeet.materialdialog.MaterialDialog;

public class LicenseActivity extends AppCompatActivity {
    ListView listView;
    List<String> licenseListName;
    private Handler handler;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView=new ListView(this);
        initLicenseName();
        handler=new Handler();
        ArrayAdapter adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, licenseListName);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> getLicense(licenseListName.get(i)));
        setContentView(listView);
    }
    private MAppLicense license;
    private void getLicense(String name){
        try{
            JSONObject json=new JSONObject();
            json.put("name",name);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.SYSTEM_LICENSE,json, new HttpInterface() {
                @Override
                public void httpGetData(String string, int code) {
                    try {
                        license= JSON.parseObject(new JSONObject(string).getJSONObject("data").toString(),MAppLicense.class);
                        handler.post(this::showDialog);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                private void showDialog(){
                    MaterialDialog dialog=new MaterialDialog(LicenseActivity.this);
                    dialog.setTitle(license.getName());
                    dialog.setMessage(license.getLicense());
                    dialog.setNegativeButton("前往Git", view -> {
                        Intent intent=new Intent(LicenseActivity.this,WebActivity.class);
                        intent.putExtra("url",license.getUrl());
                        startActivity(intent);
                        dialog.dismiss();
                    });
                    dialog.setPositiveButton("关闭",view -> dialog.dismiss());
                    dialog.show();
                }
                @Override
                public void httpError(Exception e) {

                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void initLicenseName(){
        licenseListName=new ArrayList<>();
        licenseListName.add("okhttp");
        licenseListName.add("FastJson");
        licenseListName.add("Android-ConvenientBanner");
        licenseListName.add("Android-Universal-Image-Loader");
        licenseListName.add("SmartTabLayout");
        licenseListName.add("KProgressHUD");
        licenseListName.add("ClassicLinesEditView");
        licenseListName.add("LFilePicker");
        licenseListName.add("RichText");
        licenseListName.add("dialogplus");
        licenseListName.add("QRCodeReaderView");
        licenseListName.add("PagerBottomTabStrip-master");
        licenseListName.add("RecyclerView");
        licenseListName.add("android-shape-imageview");
        licenseListName.add("glide-transformations");
        licenseListName.add("MPAndroidChart");
        licenseListName.add("Nice Spinner");
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
