package com.saki.ui;

import com.saki.qq.global.ScriptManager;
import com.saki.service.NewService;
import com.saki.ui.adapter.ScriptAdapter;
import com.saki.ui.adapter.PromoteAppAdapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.saki.sqv9.R;
import com.setqq.script.Script;

public class ScriptUI extends Bannerlist implements OnItemClickListener,
        OnItemLongClickListener, OnRefreshListener {

    public ScriptAdapter adapter;
    public SwipeRefreshLayout swipe;
    public Button renameLua;
    public Button encode;

    public ScriptUI(Context context) {
        super(context, R.layout.view_app);
    }

    @Override
    protected Banner initBanner(Context c) {
        Banner banner = (Banner) findViewById(R.id.promote);
        banner.getPager().setAdapter(new PromoteAppAdapter(c, banner));
        return banner;
    }

    @SuppressLint("InlinedApi")
    @Override
    protected ListView initList(Context c) {
        ListView list = (ListView) findViewById(R.id.applist);
        adapter = new ScriptAdapter(c, this);
        renameLua = (Button) findViewById(R.id.renameLua);
        encode = (Button) findViewById(R.id.endcode);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipe.setOnRefreshListener(this);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
        list.setOnItemLongClickListener(this);
        ScriptManager.getInstance().setListener(adapter);
        return list;
    }

    private void showUI(Script p) {
        if (p.jump) {
            if (p.packageName == null || p.packageName.equals(""))
                return;
            if (p.instance != null) {
                //弹出窗口
                Intent i = new Intent();
                i.setClass(getContext(),ScriptView.class);
                i.putExtra("script",p.packageName);
                getContext().startActivity(i);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> list, View view, int postion, long id) {
        Script p = adapter.getItem(postion);
        showUI(p);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> list, View view, int postion,
                                   long id) {
        final Script i = adapter.getItem(postion);
        if (i.packageName == null || i.packageName.equals("")) {
            Toast.makeText(getContext(), "此插件无法卸载", Toast.LENGTH_SHORT).show();
            return false;
        }
        Builder b = new Builder(getContext());
        b.setTitle("卸载提示");
        b.setMessage("确定要卸载[" + i.name + "]吗？");
        b.setNegativeButton("取消", null);
        b.setPositiveButton("卸载/删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                i.delete(getContext());
                getService().loadPlugin();
            }
        });
        b.show();
        return true;
    }

    @Override
    public void onBind(NewService service) {
        service.loadPlugin();
    }

    @Override
    public void onRefresh() {
        getService().loadPlugin();
    }

}
