package com.meiji.toutiao.setting;

import android.app.ActivityManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;

import com.afollestad.materialdialogs.color.CircleView;
import com.afollestad.materialdialogs.color.ColorChooserDialog;
import com.meiji.toutiao.Constant;
import com.meiji.toutiao.R;
import com.meiji.toutiao.util.CacheDataManager;
import com.meiji.toutiao.util.SettingUtil;
import com.meiji.toutiao.widget.IconPreference;

import de.psdev.licensesdialog.LicensesDialog;
import de.psdev.licensesdialog.licenses.ApacheSoftwareLicense20;
import de.psdev.licensesdialog.licenses.BSD3ClauseLicense;
import de.psdev.licensesdialog.licenses.MITLicense;
import de.psdev.licensesdialog.model.Notice;
import de.psdev.licensesdialog.model.Notices;

/**
 * Created by Meiji on 2017/8/5.
 */

public class GeneralPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

    private IconPreference colorPreview;
    private SettingActivity context;

    public static GeneralPreferenceFragment newInstance() {
        return new GeneralPreferenceFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        context = (SettingActivity) getActivity();
        setHasOptionsMenu(true);
        setText();

        findPreference("auto_nightMode").setOnPreferenceClickListener(preference -> {
            context.startWithFragment(AutoNightModeFragment.class.getName(), null, null, 0, null);
            return true;
        });

        findPreference("text_size").setOnPreferenceClickListener(preference -> {
            context.startWithFragment(TextSizeFragment.class.getName(), null, null, 0, null);
            return true;
        });

        findPreference("custom_icon").setOnPreferenceChangeListener((preference, newValue) -> {

            int selectValue = Integer.parseInt((String) newValue);
            int drawable = Constant.ICONS_DRAWABLES[selectValue];

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ActivityManager.TaskDescription tDesc = new ActivityManager.TaskDescription(
                        getString(R.string.app_name),
                        BitmapFactory.decodeResource(getResources(), drawable),
                        SettingUtil.getInstance().getColor());
                context.setTaskDescription(tDesc);
            }

            return true;
        });

        findPreference("color").setOnPreferenceClickListener(preference -> {
            new ColorChooserDialog.Builder(context, R.string.choose_theme_color)
                    .backButton(R.string.back)
                    .cancelButton(R.string.cancel)
                    .doneButton(R.string.done)
                    .customButton(R.string.custom)
                    .presetsButton(R.string.back)
                    .allowUserColorInputAlpha(false)
                    .show(context);
            return false;
        });

        colorPreview = (IconPreference) findPreference("color");

        findPreference("nav_bar").setOnPreferenceClickListener(preference -> {
            int color = SettingUtil.getInstance().getColor();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (SettingUtil.getInstance().getNavBar()) {
                    context.getWindow().setNavigationBarColor(CircleView.shiftColorDown(CircleView.shiftColorDown(color)));
                } else {
                    context.getWindow().setNavigationBarColor(Color.BLACK);
                }
            }
            return false;
        });

        findPreference("clearCache").setOnPreferenceClickListener(preference -> {
            CacheDataManager.clearAllCache(context);
            Snackbar.make(getView(), R.string.clear_cache_successfully, Snackbar.LENGTH_SHORT).show();
            setText();
            return false;
        });

        try {
            String version = "当前版本 " + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
            findPreference("version").setSummary(version);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        findPreference("changelog").setOnPreferenceClickListener(preference -> {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.changelog_url))));
            return false;
        });

        findPreference("licenses").setOnPreferenceClickListener(preference -> {
            createLicenseDialog();
            return false;
        });

        findPreference("sourceCode").setOnPreferenceClickListener(preference -> {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.source_code_url))));
            return false;
        });

        findPreference("copyRight").setOnPreferenceClickListener(preference -> {
            new AlertDialog.Builder(context)
                    .setTitle(R.string.copyright)
                    .setMessage(R.string.copyright_content)
                    .setCancelable(true)
                    .show();
            return false;
        });
    }

    private void setText() {
        try {
            findPreference("clearCache").setSummary(CacheDataManager.getTotalCacheSize(context));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createLicenseDialog() {
        Notices notices = new Notices();
        notices.addNotice(new Notice("PhotoView", "https://github.com/chrisbanes/PhotoView", "Copyright 2017 Chris Banes", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("OkHttp", "https://github.com/square/okhttp", "Copyright 2016 Square, Inc.", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("Gson", "https://github.com/google/gson", "Copyright 2008 Google Inc.", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("Glide", "https://github.com/bumptech/glide", "Sam Judd - @sjudd on GitHub, @samajudd on Twitter", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("Stetho", "https://github.com/facebook/stetho", "Copyright (c) 2015, Facebook, Inc. All rights reserved.", new BSD3ClauseLicense()));
        notices.addNotice(new Notice("PersistentCookieJar", "https://github.com/franmontiel/PersistentCookieJar", "Copyright 2016 Francisco José Montiel Navarro", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("jsoup", "https://jsoup.org", "Copyright © 2009 - 2016 Jonathan Hedley (jonathan@hedley.net)", new MITLicense()));

        new LicensesDialog.Builder(context)
                .setNotices(notices)
                .setIncludeOwnLicense(true)
                .build()
                .show();
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("color")) {
            colorPreview.setView();
        }
        if (key.equals("slidable")) {
            context.recreate();
        }
    }
}

