package macampcorp.macamp.activities.fragments;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import macampcorp.macamp.activities.HostActivity;
import macampcorp.macamp.activities.R;
import macampcorp.macamp.exceptions.ResourceFileNotFoundException;
import macampcorp.macamp.exceptions.ResourceNotFoundException;
import macampcorp.macamp.game.Resource;
import macampcorp.macamp.game.ResourceManager;
import macampcorp.macamp.game.challenges.Challenge;
import macampcorp.macamp.game.Indice;
import macampcorp.macamp.lang.Locale;
import macampcorp.macamp.utils.files.FileManager;
import uk.co.senab.photoview.PhotoViewAttacher;

public abstract class ChallengeFragment extends Fragment{
    public abstract int check();
    protected abstract  void onResetUi();


    public String _(int x)
    {
        return  getResources().getString(x);
    }

    protected LinearLayout  mUITextIndices;
    protected LinearLayout  mUIGlobalIndice;
    protected Challenge     mChallenge;
    protected ImageView     mImage;
    private PhotoViewAttacher mAttacher;

    public HostActivity getHostActivity()
    {
        return HostActivity.cast(getActivity());
    }

    protected void onInitChallenge(Challenge c)
    {
        if(c!=null) setImageResource(c.getResourceName());
    }

    public void onChallengeFailed()
    {

    }

    public void onChallengePassed()
    {

    }

    public boolean onBackPressed()
    {
        return true;
    }

    protected void setImageResource(String res)
    {
        Resource r = ResourceManager.getInstance().get(res);

        if(r==null)
        {
            throw new ResourceNotFoundException(res);
        }

        if( r.isImage() )
        {
            String name = r.getName();
            String path = r.getFile();

            Bitmap bmp=null;
            try {
                 bmp = FileManager.openImage(getActivity(), path);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ResourceFileNotFoundException(r);
            }

            mImage.setImageBitmap(bmp);
            mAttacher = new PhotoViewAttacher(mImage);
            mAttacher.setMaximumScale(20);
        }
    }

    public void resetUi()
    {
        getHostActivity().setVisibilityView(R.id.btn_indice, View.VISIBLE);
        if(mUITextIndices!=null)
        {
            mUITextIndices.removeAllViews();
        }
        if(mUIGlobalIndice!=null)
        {
            mUIGlobalIndice.setVisibility(View.GONE);
        }
        onResetUi();
    }

    protected TextView createTextView(Indice ind, int indInd)
    {
        TextView tv = new TextView(getActivity());
        tv.setTextColor(0xff70e030);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        tv.setText(_(R.string.indice_prefix)+ " "+ind.getText());
        tv.setTypeface(null, Typeface.BOLD);
        return  tv;
    }

    private void updateIndices()
    {
        ArrayList<Indice> inds = mChallenge.getIndices();
        int indInd = mChallenge.getIndiceIndex();
        if(inds.size()>0) mUIGlobalIndice.setVisibility(View.VISIBLE);
        for(int i=0; i<inds.size(); i++)
        {
            Indice ind = inds.get(i);
            if(!ind.isDiscovered()) return;
            mUITextIndices.addView(createTextView(ind, indInd));

        }
        //si on arrive ici, ca veut dire que tous les indices ont ete découvert
        getHostActivity().setVisibilityView(R.id.btn_indice, View.INVISIBLE);
    }

    public Indice showIndice()
    {
        Indice ind = mChallenge.nextIndice();
        int indInd = mChallenge.getIndiceIndex();
        if(ind!=null)
        {


            if(mUITextIndices!=null) {
                mUIGlobalIndice.setVisibility(View.VISIBLE);
                mUITextIndices.addView(createTextView(ind, indInd));
                ind.discover();
                return ind;
            }
        }
        return null;
    }





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,int res) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(res, container, false);
        mUITextIndices= (LinearLayout) v.findViewById(R.id.ll_textIndices);
        mUIGlobalIndice = (LinearLayout) v.findViewById(R.id.ll_rootIndices);
        mImage = (ImageView) v.findViewById(R.id.iv_img);
        return v;
    }


    public void initChallenge(Challenge c)
    {
        if(c==null)
        {
            getHostActivity().setFinished();
        }
        mChallenge=c;
        resetUi();
        if(mChallenge!=null && !mChallenge.hasNextIndice())
        {
            getHostActivity().setVisibilityView(R.id.btn_indice, View.INVISIBLE);
        }
        onInitChallenge(c);
    }






    protected void ready()
    {
        getHostActivity().onReady(getHostActivity().currentChallenge());
    }

    @Override
    public void onStart() {
        super.onStart();
        ready();
    }


    public void onResume() {
        super.onResume();
        if(mChallenge!=null) updateIndices();
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    private OnFragmentInteractionListener mListener;


    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public Challenge getChallenge() {
        return mChallenge;
    }




    //
    // Fin de Timer géolacalisation
    //


}
